Alpha Video Training
Results 1 to 8 of 8

Thread: Ajax Callback / Function

  1. #1
    Member
    Real Name
    Neville Dearham
    Join Date
    Jul 2015
    Location
    Cape Town South Africa
    Posts
    91

    Default Ajax Callback / Function

    UX Control
    It is my first attempt at Ajax callbacks and function.

    Made a function library with a function:

    FUNCTION oac_CongrsApproved as c (e as p)
    dim cn as sql::connection
    dim sqlcommand as c
    dim rs as sql::ResultSet
    dim args as sql::Arguments
    dim result as c
    dim Return_oac_CongrsApproved as c
    args.add("UserID", "neville.dearham@oacwc.net")
    sqlcommand="select ResCongrNo from oac_Users_CongrsApproved where UserID=:args.UserID"
    cn.execute(sqlcommand)
    Return_oac_CongrsApproved = rs.toString()
    ?Return_oac_CongrsApproved
    end function

    I call this with an Action Javascript on the detail view in the AfterDetailViewPopulate embed point.
    When I fill out the dialog for an Ajax callback the function select does not show me the functions(I typed it in by hand). What am I doing wrong
    AjaxCallBack.JPG
    Question: The string it must return, how do I get it back to the Detail view so that I can interrogate the string with a js "include" function
    Any help appreciated.
    Regards

  2. #2
    "Certified" Alphaholic mikeallenbrown's Avatar
    Real Name
    Mike Brown
    Join Date
    Nov 2009
    Location
    United States
    Posts
    1,746

    Default Re: Ajax Callback / Function

    Who knows why it doesn't show in the list. Works on my end (using Alpha 5647). Doesn't matter though as the name of the function is already there in the Function Name box. Just press ok.

    Also, ?Return_oac_CongrsApproved isn't going to work in this instance. And how you're using the arg in sqlcommand isn't correct.

    Updated code...

    Code:
    FUNCTION oac_CongrsApproved as c (e as p)
    
    dim cn as sql::connection
    dim sqlcommand as c
    dim rs as sql::ResultSet
    dim args as sql::Arguments
    dim result as c
    dim Return_oac_CongrsApproved as c
    
    args.add("UserID", "neville.dearham@oacwc.net")
    
    sqlcommand="select ResCongrNo from oac_Users_CongrsApproved where UserID=:UserID"
    
    if cn.Open(cn) then
    	if cn.Execute(sqlcommand,args) then
    		'Here is my data
    		rs=cn.ResultSet
    	else
    		'SQL statement failed
    		cn.Close()
    		oac_CongrsApproved = "alert('SQL Statement failed.');"
    		exit function
    	end if
    else
    	'couldn't connect to SQL DB
    	oac_CongrsApproved = "alert('No SQL connection');"
    	exit function
    end if
    
    cn.FreeResult()
    cn.Close()
    
    'Not going to work
    'Return_oac_CongrsApproved = rs.toString()
    
    'If you want/need the toString function that this is what you want.
    'Replace the my data line, above, with the line below.
    'rs = cn.ResultSet.toString()
    
    'This isn't going to work
    '?Return_oac_CongrsApproved
    
    'Emit something to the browser. In this case an alert.
    oac_CongrsApproved = "alert('Hello!');"
    
    end function
    It's a best practice to also include error handling. What happens if what SQL returns is empty or invalid or the SQL connection didn't work?

    I didn't understand the last part. What is it you're trying to do?
    Last edited by mikeallenbrown; 02-11-2019 at 08:36 PM. Reason: syntax errors
    Mike Brown - Contact Me
    Programmatic Technologies, LLC
    Programmatic-Technologies.com
    Independent Developer & Consultant

  3. #3
    Member
    Real Name
    Neville Dearham
    Join Date
    Jul 2015
    Location
    Cape Town South Africa
    Posts
    91

    Default Re: Ajax Callback / Function

    Users are allocated records they are allowed to edit.
    So when they open the detail view I do an Ajax callback to return a string of the ResCongrNo's.
    I check to see if the ResCongrNo in the Detail View is included in the string returned. I set a logical value to a control on the detailview Whether it contains the substring or not. If the value=0 I hide the CRUD buttons if 1 the buttons are enabled and the user can edit the record.

  4. #4
    "Certified" Alphaholic mikeallenbrown's Avatar
    Real Name
    Mike Brown
    Join Date
    Nov 2009
    Location
    United States
    Posts
    1,746

    Default Re: Ajax Callback / Function

    There are lots of ways to go about doing things. A callback every time a user clicks on a record is one way. But it's not very efficient or fast. What about running this SQL statement when the component loads? Get this ResCongrNo you need in the beginning and when the user clicks on a record a snippet of JS can handle the rest.

    Another way might be to work the oac_Users_CongrsApproved table into the query that is populating your list. This is a UX, right? I'm assuming as much. Nonetheless, if this was a grid these methods apply still. Just different syntax.
    Mike Brown - Contact Me
    Programmatic Technologies, LLC
    Programmatic-Technologies.com
    Independent Developer & Consultant

  5. #5
    Member
    Real Name
    Neville Dearham
    Join Date
    Jul 2015
    Location
    Cape Town South Africa
    Posts
    91

    Default Re: Ajax Callback / Function

    I was just testing the code and was planning to move this function to when the user logs in. Then the string returned must be global and I only do one callback. So the string remains for the duration of the session and then test the string with a java script "include" function each time a detailview is opened.
    I do not want to filter the list as I want all users to read all the data, but only allowed to edit certain records allocated to them.
    I will be using this function on many other places in the project so it needs to be global.
    Many thanks Mike...I have learned a lot here. off to bed now been a long day. Talk again tomorrow if that is in order
    Cheers

  6. #6
    Member
    Real Name
    Neville Dearham
    Join Date
    Jul 2015
    Location
    Cape Town South Africa
    Posts
    91

    Default Re: Ajax Callback / Function

    Should I now put this ajax call after the user has logged as I now the have the userid (session var at this moment) how would I change the code after the ajax callback to make the returning string global for further reference during the session

  7. #7
    "Certified" Alphaholic mikeallenbrown's Avatar
    Real Name
    Mike Brown
    Join Date
    Nov 2009
    Location
    United States
    Posts
    1,746

    Default Re: Ajax Callback / Function

    Not knowing anything about your app that's a hard question to answer. Do you need this ResCongrNo in other places in the app? Or is it only used in the component where user's edit/view records?

    One way to send this ResCongrNo data to the browser for use in a JS function would be this...

    Code:
    FUNCTION oac_CongrsApproved as c (e as p)
    
    dim cn as sql::connection
    dim sqlcommand as c
    dim rs as sql::ResultSet
    dim args as sql::Arguments
    dim result as c
    dim Return_oac_CongrsApproved as c
    
    args.add("UserID", "neville.dearham@oacwc.net")
    
    sqlcommand="select ResCongrNo from oac_Users_CongrsApproved where UserID=:UserID"
    
    if cn.Open(cn) then
    	if cn.Execute(sqlcommand,args) then
    		'Here is my data
    		rs=cn.ResultSet.toString()
    	else
    		'SQL statement failed
    		cn.Close()
    		oac_CongrsApproved = "alert('SQL Statement failed.');"
    		exit function
    	end if
    else
    	'couldn't connect to SQL DB
    	oac_CongrsApproved = "alert('No SQL connection');"
    	exit function
    end if
    
    cn.FreeResult()
    cn.Close()
    
    dim js as c = ""
        js = "{dialog.object}.__approved = '__DATA__';"
    
    js = stritran(js,"__DATA__",rs)
    
    'Emit something to the browser. In this case an alert.
    oac_CongrsApproved = js
    
    end function
    JS function code...

    Code:
    function canEdit(){
    
    var _data = {dialog.object}.__approved
    
      if(_data.includes('yourdata')){
         //Do something here
      }
    
    }
    Now you could use the Show/hide expression property for the controls you want to hide. Or, use the enable expression property for controls you want.

    Also, I free wrote that code from memory. Syntax might not be perfect. Just FYI.
    Mike Brown - Contact Me
    Programmatic Technologies, LLC
    Programmatic-Technologies.com
    Independent Developer & Consultant

  8. #8
    Member
    Real Name
    Neville Dearham
    Join Date
    Jul 2015
    Location
    Cape Town South Africa
    Posts
    91

    Default Re: Ajax Callback / Function

    Many thanks, will check it out

Similar Threads

  1. Ajax callback in calculated field function?
    By WindForce in forum Mobile & Browser Applications
    Replies: 1
    Last Post: 08-22-2016, 05:46 PM
  2. Ajax Callback - to run a function on the server
    By tsyeti in forum Application Server Version 11 - Web/Browser Applications
    Replies: 3
    Last Post: 08-16-2012, 04:13 PM
  3. Error executing Ajax callback function
    By eritovs in forum Application Server Version 10 - Web/Browser Applications
    Replies: 3
    Last Post: 07-06-2010, 04:03 AM
  4. using Ajax callback function to get mysql data
    By eritovs in forum Application Server Version 10 - Web/Browser Applications
    Replies: 0
    Last Post: 06-11-2010, 12:10 AM
  5. V10 Ajax Callback to XBasic Function
    By ismac in forum Application Server Version 10 - Web/Browser Applications
    Replies: 2
    Last Post: 07-29-2009, 05:52 AM

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
  •