[jQuery] Ajax call Scope - Module Pattern and non singleton pattern
Hi, my problem is due to the scope. Using the module pattern following
the Eric´s design explained on the Amazon Books widget example(http://
net.tutsplus.com/tutorials/javascript-ajax/create-an-amazoncom-books-
widget-with-jquery-and-xml/) we have something similar to this:
var BOOKS = function(){
var _P = {
init : function( params ) {},
params : null,
data : null,
loadXml : function() {
$.ajax({
type : "GET",
url : _P.params.xmlPath,
dataType : "xml",
success : function( data ) {
_P.data = data;
_P.max = _P.params.perView;
_P.count = $( "book", data ).length;
_P.preloadBooks();
_P.browseBooks();
}
});
},
first : 0,
max : 0,
count : 0,
preloadBooks : function() {},
browseBooks : function( browse ) {},
tooltip : {
show : function( e, $o ) {},
hide : function( e, $o ) {},
getMouseCoord : function( v, e ) {},
getViewport : function() {}
}
};
return {
init : function( params ) {
_P.init( params );
}
};
}();
And it works, perfectly, after we can access to any _P attribute with
the use of _P.xxxx in any part of the code, and the call works
perfectly saving the value of the ajax call. The problem is that this
pattern creates a singleton instance, so it is not possible to use
more than one widget on a same web.
Which patter could we use to give it multiple instances and be able to
use several widget?
I tried several ways but i can never save the async call into
variables, always undefined result.
Thanks indeed.