Page 287 - Asterisk™: The Future of Telephony
P. 287

Next, we define a function called localAjaxinit. The localAjaxinit function first tells
               this page’s parent (cfgbasic.html in this case) to run the loadscreen function, passing in
               this  page  as  the  parameter.  This  causes  the  main  GUI  screen  to  load  our  new
               test.html inside the iframe. The second thing we do inside the localAjaxinit function
               is to use the makerequest function. The makerequest function is defined in astman.js and
               makes it very convenient to make requests to the web server. ‖

               The first parameter of the makerequest function specifies what type of request is being
               made. It can be set to any of the following:

               'g'
                   Use the GetConfig action to retrieve the configuration from the configuration file
                   specified in the second parameter.
               'u'
                   Use the UpdateConfig action to update the configuration in the configuration file
                   specified in the second parameter. The third parameter to the function specifies
                   the configuration data that should be updated.
               ''
                   If the first parameter to the makerequest function is a set of single quotes, then the
                   custom action specified in the third parameter will be sent.
               The fourth parameter is the callback function that will be called with the response to
               the Ajax request.

                                     Examples of Using makerequest

                  As  an  example,  the  following  code  snippet  shows  three  different  ways  to  use  the
                  makerequest function. In the first, we’ll get the configuration data from users.conf. In
                  the second, we’ll update musiconhold.conf and change the value of the random setting
                  in the default class. Last but not least, we’ll call the Ping action. Each of them sets a
                  callback function named t that simply replaces the contents of the div with the response
                  of the Ajax call.
                     makerequest( 'g', 'users.conf', '' ,
                         function(t) { $('ExtensionsDotConf').innerHTML = "<PRE>" + t +
                         "</PRE>"; } );
                     makerequest( 'u', 'musiconhold.conf',
                     '&Action-000000=update&Cat-000000=default&Var-000000=random&Value-
                         000000=yes' ,
                         function(t) { $('ExtensionsDotConf').innerHTML = "<PRE>" + t +
                         "</PRE>"; } );

                     makerequest( '', '', 'action=Ping' ,
                        function(t) { $('ExtensionsDotConf').innerHTML = "<PRE>" + t +
                        "</PRE>"; } );



               ‖ In reality, makerequest is a simple wrapper around a call to Prototype’s Ajax.Request method.

                                                                Developing for the Asterisk GUI | 259
   282   283   284   285   286   287   288   289   290   291   292