Alpha Video Training
Results 1 to 3 of 3

Thread: Solution for passing variables between client and server in ajax callback!

  1. #1
    Member nlights's Avatar
    Real Name
    pertti karjalainen
    Join Date
    May 2011
    Location
    California
    Posts
    217

    Default Solution for passing variables between client and server in ajax callback!

    Just in case someone else is struggling with the same thing as I've been struggling for a while, this explanation might save you some time and tufts of hair.

    Task: Grid1 row calls Grid2, and Grid2 needs to know the primary key (row ID) of the calling row in Grid1.

    First solution: Create a session variable during the call from Grid1, and store the primary key in that variable. Then pick up the session variable in Grid2

    Problem with the first solution: Session variables are often cached in the browser, so while the first call from Grid1 sends the right primary key to Grid2, subsequent calls that try to refresh the session variable with a new primary key don't show up in the browser, because the browser grabs the value from the cache, which is the previous value

    Second solution: Create a variable and attach it to the browser's window object. Then pick up the window object variable in Grid2. Since the window object is not cached, refreshing the value of the attached variable succeeds every time, and thus Grid2 receives the right ID every time

    Problem with the second solution:
    While it is easy enough to display window[] variables in a freeform HTML section, it is quite a different story trying to tell a server-side Xbasic function what those variables are. I've seen some pretty creative solutions having to do with storing a variable to a calculated hidden grid column etc., but they all had inherent problems for my purposes, so the search continued. And continued. And continued. Until finally I figured out how to...

    How to do it anyway:

    1. During the ajax callback call from a button on Grid1 row to Grid2 create a window variable in inline-javascript such as:
    Code:
    window['wvE_EVENTID'] = {grid.Object}.getValue('G','E_EVENTID', {Grid.RowNumber});
    followed by "Open a Grid Component" Action Script

    2. In grid2 you can use an action button's javascript to call an xBasic function that receives the window[] variable as follows:
    Code:
    var rowNum = {grid.object}._selectedRow;
    var additionaldata='vendorid ='+window['wvE_EVENTID'];
    {grid.Object}.ajaxCallback('G',rowNum,'InsertData','','_getData=true&'+additionaldata);
    The trick here, of course, is to use the "Additional data to submit" -feature of the ajax callback action javasript. And that is what allows Javascript communicate with Xbasic.

    When you examine the e. object properties in the InsertData Xbasic function with the debugger, you will see that one of the variables passed as its property is "e.vendorid_", which has the value of window['wvE_EVENTID'], which in turn was set in Grid1. So now you can have your parent grid primary key and eat it too!

    And who says an old dog can't learn new tricks?

    Thank you. Thank you very much.

  2. #2
    "Certified" Alphaholic peteconway's Avatar
    Real Name
    Peter Conway
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    2,627

    Default Re: Solution for passing variables between client and server in ajax callback!

    Thanks for the excellent post, one of the best presented I have ever read. The Wiki could certainly use learn from your style and clarity.
    Insanity: doing the same thing over and over again and expecting different results.
    Albert Einstein, (attributed)
    US (German-born) physicist (1879 - 1955)

  3. #3
    Member nlights's Avatar
    Real Name
    pertti karjalainen
    Join Date
    May 2011
    Location
    California
    Posts
    217

    Default Re: Solution for passing variables between client and server in ajax callback!

    Thank you, and I fully agree with your Wiki comment.

Similar Threads

  1. Callback value passing
    By eribla in forum Application Server Version 10 - Web/Browser Applications
    Replies: 2
    Last Post: 12-12-2011, 08:33 PM
  2. Passing contents of <form> input to Ajax callback
    By Antony in forum Application Server Version 10 - Web/Browser Applications
    Replies: 8
    Last Post: 11-01-2011, 06:36 PM
  3. Ajax Callback
    By lvasic in forum Application Server Version 10 - Web/Browser Applications
    Replies: 2
    Last Post: 08-18-2011, 09:35 AM
  4. Trouble with Session Variables, AJAX Callbacks, and AJAX Popup Windows
    By njw1314 in forum Application Server Version 10 - Web/Browser Applications
    Replies: 7
    Last Post: 07-20-2011, 11:53 PM
  5. Ajax Callback from Client Side Grid System Event
    By kingcarol in forum Application Server Version 10 - Web/Browser Applications
    Replies: 12
    Last Post: 08-27-2010, 09:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •