admin管理员组文章数量:1415137
I have this kind of ajax code repeated lot of places. How can I refactor this into a single method so it will still allow different behavior on success or failure.
Ext.Ajax.request({
url : 'ajax.php' ,
params : { action : 'getDate' },
method: 'GET',
success: function ( result, request ) {
Ext.MessageBox.alert('Success', 'Data return from the server: '+ result.responseText);
},
failure: function ( result, request) { Ext.MessageBox.alert('Failed', result.responseText);
}
});
I have this kind of ajax code repeated lot of places. How can I refactor this into a single method so it will still allow different behavior on success or failure.
Ext.Ajax.request({
url : 'ajax.php' ,
params : { action : 'getDate' },
method: 'GET',
success: function ( result, request ) {
Ext.MessageBox.alert('Success', 'Data return from the server: '+ result.responseText);
},
failure: function ( result, request) { Ext.MessageBox.alert('Failed', result.responseText);
}
});
Share
Improve this question
edited Jan 31, 2011 at 4:50
fastcodejava
asked Jan 30, 2011 at 23:44
fastcodejavafastcodejava
41.2k31 gold badges139 silver badges191 bronze badges
3 Answers
Reset to default 3MyAjaxRequest = Ext.extend ( Ext.Ajax.request, { url : 'ajax.php' , params : { action : 'getDate' }, method: 'GET', success: function ( result, request ) { Ext.MessageBox.alert ('Success', 'Data return from the server: '+ result.responseText); }, failure: function ( result, request) { Ext.MessageBox.alert('Failed', result.responseText); } } );
by extending class (namespaces up to you) you still able to manipulate url, params, method, success, and failure. if not setup - defaults are there
Okay, this question is kind of old, but there arguably a more flexible way to do this. It's really important to realize that Ext.Ajax is a singleton -- that is, it is already a unique pre-instantiated class. "Extending" a singleton doesn't make much sense, and a separate function may be unnecessarily confusing and/or limiting later on.
You can add your own special Ajax request function like this:
Ext.Ajax.dateRequest = function(myObj){
// set the pre-configured parameters here
myObj.url = 'ajax.php';
myObj.params = { action: 'getDate'};
myObj.method = 'GET';
// call the original request function with the modified config object
this.request(myObj);
};
So now you can change your repeated Ajax requests to:
Ext.Ajax.dateRequest({
success: yourSuccessFunction
,failure: yourFailureFunction
});
The benefit to this is that you can easily add pre-configured parameters to your "dateRequest" function, AND you can add addition parameters to each Ajax request (like a different timeout) without rewriting anything.
EDIT: Yikes! I originally posted a solution below that I thought would "clone" Ext.Ajax, but it still merely overrode the singleton.
This is a quote by "Saki" (Ext guru) a couple of years ago. He's referring to a clone function he wrote for regular object/arrays:
The clone function is in no case meant to clone classes or instantiated Ext objects. It is almost impossible as these install event handlers almost always so cloning would definitely lead to unpredictable results.
The singleton is a "instantiated Ext object" and thus cannot be extended or cloned easily. If you don't want to mess with Ext.Ajax directly, you can create a function (as already mentioned). Here is a somewhat more flexible form:
function dateRequest(myObj){
myObj.url = 'ajax.php';
myObj.params = { action: 'getDate'};
myObj.method = 'GET';
return Ext.Ajax.request(myObj);
}
Then call it with dateRequest({success: successFunc, failure: failFunc})
.
This code will achieve the same result:
function callme (callback) {
Ext.Ajax.request({
url : 'ajax.php' ,
params : { action : 'getDate' },
method: 'GET',
success: callback,
failure: function ( result, request) { Ext.MessageBox.alert('Failed', result.responseText);
}
});
}
callme(function ( result, request ) {
Ext.MessageBox.alert('Success', 'Data return from the server: '+ result.responseText);
});
本文标签: javascriptExtjs ajax code refactorStack Overflow
版权声明:本文标题:javascript - Extjs ajax code refactor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745200924a2647364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论