Context of callback

May 30, 2011 at 2:13 PM

When making an ajax call to retrieve a list of items:

this.webServiceCall = function() {

$j().SPServices({
            operation: "GetListItems",
            weburl:this.protocol+this.server+this.site,
            listName: this.listGUID,
            viewName: this.viewGUID,
            async:true,
            completefunc: this.SPCallback
        });

}

The context of the callback is lost.

In jQuery there is a special paramater to keep it: context

 

. . .

this.SPCallback = function(){ . . .},
this.ajaxFailure= function(){ . . .},

this.webServiceCall = function() {
            $j.ajax({
                context:this,
                url: this.dataURL,   
                dataType: "xml",
                success: this.SPCallback,
                error: this.ajaxFailure
            });

}

. . .

 

Most of the time the SharePoint webservices calls are made by an object not by a stand alone piece of JavaScript like in all the examples provided in the documentation.

How to be sure once the code of SPCallback method is executed it's done in the context of the Object encapsulation the werb service call and not the context of the XML answer ?

With jQuery it works with SPServices I don't know hot to do it.

May 30, 2011 at 2:34 PM
grumelo wrote:
Edit feature is not working I can not fix a typo in the original message (this kind of bug drive me crazy!) So here is the same message but fixed

When making an ajax call to retrieve a list of items:

this.webServiceCall = function() {

$j().SPServices({
            operation: "GetListItems",
            weburl:this.protocol+this.server+this.site,
            listName: this.listGUID,
            viewName: this.viewGUID,
            async:true,
            completefunc: this.SPCallback
        });

}

The context of the callback is lost.

In jQuery there is a special paramater to keep it: context

 

. . .

this.SPCallback = function(){ . . .},
this.ajaxFailure= function(){ . . .},

this.webServiceCall = function() {
            $j.ajax({
                context:this,
                url: this.dataURL,   
                dataType: "xml",
                success: this.SPCallback,
                error: this.ajaxFailure
            });

}

. . .

 

Most of the time the SharePoint webservices calls are made by an object not by a stand alone piece of JavaScript like in all the examples provided in the documentation.

How to be sure once the code of SPCallback method is executed it's done in the context of the Object encapsulating the web service call and not the context of the XML answer ?

With jQuery it works with SPServices I don't know how to do it.

 

Coordinator
May 31, 2011 at 2:54 AM

grumelo:

I've intentionally built SPServices this way. It's very unusual to call a Web Service with SPServices and require a callback. The way I've built it, you do your processing when the AJAX call completes in the completefunc.

I'd be interested in any specific examples you might have where this approach won't work.

M.

May 31, 2011 at 7:17 AM

The SPServices is used in a complexe client side framework.

The method that has to handle the XML once it's back is part of this framework.

Async or not does not matter. My issue is the keep the context of the Object where SPServices is called from.

I now have a solution thanks to closure.

It's something like this:

. . .

this.SPCallback = function(){ . . .},
this.getData = function() {

$j().SPServices({
            operation: "GetListItems",
            weburl:this.protocol+this.server+this.site,
            listName: this.listGUID,
            viewName: this.viewGUID,
            async:true,
            completefunc: function(context){return function(param1,param2){context.SPCallback(param1, param2);}}(this)
        });

}

. . .

 

Do you understand the solution?

Coordinator
May 31, 2011 at 12:13 PM

I'm not sure exactly what it is that you're trying to do, but if it's working for you, then I guess you're good!

M.