I define a class like this:
var LoadingPolecy={
initialize:function(){
return function(){
this.initialize.apply(this,arguments);
}
}
}
var AjaxLoadingPolecy= LoadingPolecy.initialize();
AjaxLoadingPolecy.prototype={
initialize:function(name){
this.name=name;
},
AjaxStartPolecy : function(){
...
},
AjaxStopPolecy : function(){
...
},
SetName : function(name){
...
}
}
var TempLoadingPolecy=LoadingPolecy.initialize();
TempLoadingPolecy.prototype={
initialize:function(displayArea,source,data){
this.loadingMsgPolecy = new AjaxLoadingPolecy();
...
},
StartLoadingTempPolecy : function(callback){
this.loadingMsgPolecy.SetName('view');
this.loadingMsgPolecy.AjaxStartPolecy();
var a = $.ajax({
...
success:function(html){
callback(html);
}
});
},
EndLoadingTempPolecy : function(html){
//Cannot call method 'AjaxStopPolecy' of undefined error
this.loadingMsgPolecy.AjaxStopPolecy();
....
}
}
I seems the object in this has been changed, how can I call/ use the variables that I defined in initialize?
The ajax success callback (and the same for most other callbacks) does not give you the same
this. But, you can save the previous copy ofthisinto another variable and access it that way:I don’t understand which part of your code you are asking about, but here’s a simple example that you can adapt from: