Monday, March 26, 2012

Callback Scope on the client

I've started to play around w/ some of the basic Atlas features, mainly calling a web service from the client. One thing I noticed is that scope is lost during the asynchronous call to the server. If my javascript caller & callback method are part of an class, I've no way of telling Atlas to invoke the callback method w/ a specific object's scope.

Is this possible to do?

Cheers,
BobbyYou have a userContext parameter which you can pass as part of yourasynchronous call, that is what you can use to get back to the contextof your call, i.e. you can pass the object who's scope you want back aspart of the call...

Hope that helps,
-Hao
As I understood it, the userContext serves as a holder for contextual data that can be used inside the handler as an object - scope is still lost. Consider the following js class example:

TestClass = function(prop1)
{
this.propertyOne = prop1;

this.test = function()
{
MyCallbackServiceClass.Test({onMethodComplete: this.testCallback, userContext: this});
}

this.testCallback = function(result, user)
{
alert(this.propertyOne);
}
};

var t = new TestClass("This should persist before and after callback");
t.test();

When propertyOne is alerted in the testCallback method, it will be undefined because the scope is not preserved. I don't want the object as a parameter - I want to be operating within that object.
Mike Harder from the ASP.NET team, being the top-notch guy he is, answered this question for me. The scope is maintained by using a delegate.

CSharper.Net.CallbackService.Test({ onMethodComplete: Function.createDelegate(this, this.testCallback), userContext: this });

No comments:

Post a Comment