Succeed callback in javascript class

Last post 03-17-2008 11:07 AM by mrmercury. 1 replies.

Sort Posts:

  • Sad [:(] Succeed callback in javascript class

    03-17-2008, 9:30 AM
    • Loading...
    • ChoKamir
    • Joined on 12-13-2006, 8:26 PM
    • Posts 11

    Dear fellows,

     

    I've a javascript function with retreives a object from the HTML page and calls a webservice. The result function called by the webservice needs the object retreived. How can i do this?

    What I thought off, but is not working is the following sollution. The first function creates and javascript class and stores the reference to the object in that class. The class contains also the result function with will be called by the webservice. But unfortunatly the object is not kept.

    Here is some example code:

     

    <script language="javascript" type="text/javascript">
    //global reference to the class
    var globalClass;
    
    //The class which stores the reference to the object and 
    //contains the function with handles the result returned form the webservice
    function resultClass(object) 
    {
         //the reference to the object
         this.object = object;
         //the result function
         this.result = function (arg) {
              alert(this == globalClass);
              alert(object);
              alert(arg);
         }
    }
    
    function problem(arg)
    {
         alert("There was a problem with the webservice:"+arg);
    }
    
    function doService()
    {
         //gets the object and stores the reference in the variable thing
         var thing = retreiveObject()
         //creates a new class
         globalClass = new resultClass(thing);
         //tests if everything is working
         globalClass.result("test");
         //result: first alert = true
         //second alert = the content of thing
         //third alert = test
         //so globalClass is working correctly
     
         //call the webservice
         MyWebserice.MyMethod(globalClass.result,problem,problem);
         //result: first alert = false
         //second alert = null
         //third alert = result webservice call
         //so it is not working
    }
    </script>
    
     

    What am I doing wrong?

     

    Thanks for all the help,

     

    Jelmer

  • Re: Succeed callback in javascript class

    03-17-2008, 11:07 AM
    Answer
    • Loading...
    • mrmercury
    • Joined on 04-04-2006, 6:26 PM
    • Mexico City, Mexico
    • Posts 319

    You have the right idea but ASP.NET AJAX has a specific way of creating classes, well in reality JavaScript has it, you should use prototypes to implement your classes, check this link:

    http://www.asp.net/AJAX/Documentation/Live/tutorials/ASPNETAJAXAndJavaScriptTutorials.aspx

    Here’s an example that should do what you want:

    1    Type.registerNamespace("Test");
    2    
    3    Test.MyClass = function() {
    4        Test.MyClass.initializeBase(this);
    5    }
    6    Test.MyClass.prototype = {
    7        dispose : function() {        
    8            Test.MyClass.callBaseMethod(this, "dispose");
    9        },
    10       
    11       initialize : function() {
    12           Test.MyClass.callBaseMethod(this, "initialize");
    13           
    14           MyWebserice.MyMethod(Function.createDelegate(this, this._myMethodCallbackSucceeded), Function.createDelegate(this, this._myMethodCallbackFailed));
    15       },
    16       
    17       _myMethodCallbackSucceeded : function(result) {
    18           alert(result);
    19       },
    20       
    21       _myMethodCallbackFailed : function(error) {
    22           alert(error.get_exceptionType()):
    23       }
    24   }
    25   Test.MyClass.registerClass("Test.MyClass", Sys.Component);
    26   
    27   function pageLoad() {
    28       main = new Test.MyClass();
    29       main.initialize();    
    30   }
    31   
    32   if(typeof(Sys) !== "undefined")
    33       Sys.Application.notifyScriptLoaded();
    
     

    Some notes:
    - You should use an external .js file for this, load it using the ScriptReference tag of you page’s ScriptManager.
    - Notice that you should register a namespace at the beginning
    - You can change the name of the namespace (in this case Test) and the name of the class (in this case MyClass) to whatever you want.
    - As you can see there’s a pageLoad function, this function gets called when the page loads and calls the initialize function of your class.
    - The las two lines of code are for telling ASP.NET AJAX that a new class has been loaded.
    - Your webservice gets called in the initialize function of your class.
    - As you can see I’m using delegates when asigning the callback functions, this is to maintain the context of the call, otherwise you won’t be able to use “this” in the success callback function.

    Hope this helps,

    Elias.

    If this post helped you please remember to set it as Answer so it can help others.
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter