Best Pratice: How to handle Web Service return object?

Rate It (1)

Last post 07-15-2007 3:58 PM by KaziManzurRashid. 1 replies.

Sort Posts:

  • Best Pratice: How to handle Web Service return object?

    07-15-2007, 11:59 AM
    • Member
      42 point Member
    • Spongebob76
    • Member since 02-21-2004, 8:29 PM
    • Posts 19

    Hi,

    I have a general question about Web Services in MS Ajax.
    The code snippet below shows a simple JavaScript class.

    Type.registerNamespace("Namespace");
    
    Namespace.Class= function(MainTagID) {
      this.MainTagID = 1;
      this.SomeOtherValue = "whatever";
    }
    
    Namespace.Class.prototype = {
    
      GetTagObject: function()
      {
         Namespace.Class.GetTagObject(this.MainTagID, param1, param2, 
            param3, this._onReceiveTagObject, this.onFailedCallback);
      },
    
      // This is the callback function that
      // processes the Web Service return value.
      _onReceiveTagObject: function(TagObject)
      {
        alert(this.SomeOtherValue)
    
      }
    
      ...
    
    }
    Let's say I create a new object and call the GetTagObject method.
    var x = new Namespace.Class()
    x.GetTagObject();
    After a succesful WebService request, onReceiveTagObject gets called which returned a TagObject from the server.
    However, I have lost all information about the client side JavaScript class (alert(this.SomeOtherValue) fires an error)
    Is there an elegant solution to solve this?
    My ideas so far:
    1) Pass all the variables that I need on a return as additional payload (a lot of work)
    2) Create a global variable and pass the name of the instance as an additional parameter and call something like eval(TagObject.MyInstanceName + '.myFunction()') which is not a very elegant solution in my view.
    Any thoughts on this one?
    Thanks,
    Andre
  • Re: Best Pratice: How to handle Web Service return object?

    07-15-2007, 3:58 PM
    Answer
    • Contributor
      4,792 point Contributor
    • KaziManzurRashid
    • Member since 03-09-2003, 11:04 AM
    • Dhaka, Bangladesh
    • Posts 882

    The onComplete function is not executing in the same context. Use the Function.createDelegate to resolve the this keyword. Try the following code:

     

    Namespace.Class.prototype = {
    
      GetTagObject: function()
      {
    	var handler = Function.createDelegate(this, this._onReceiveTagObject);
    
         Namespace.Class.GetTagObject(this.MainTagID, param1, param2, 
            param3, handler, this.onFailedCallback);
      },
    
      // This is the callback function that
      // processes the Web Service return value.
      _onReceiveTagObject: function(TagObject)
      {
        alert(this.SomeOtherValue)
    
      }
    
      ...
    
    }
     
    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
Page 1 of 1 (2 items)