Wednesday, May 23, 2007

Using AjaxPro with Ext data store

As I posted on Ext forum I use the following modified code to use AjaxPro with Ext. It helps avoid emedding URLs in the proxy call.

ds = new Ext.data.Store({
proxy: new Ext.data.AjaxProxy(myAsp, "GetMovies"),
reader:reader,
remoteSort: true
});


And AjaxProProxy.js, modified from Rodiniz:

Ext.data.AjaxProxy = function(ajaxProObject, method) {
Ext.data.AjaxProxy.superclass.constructor.call(this);
this.ajaxProObject = ajaxProObject;
this.method = method;
};
Ext.extend(Ext.data.AjaxProxy, Ext.data.DataProxy, {

// Harley's load function
load: function(params, reader, callback, scope, arg) {
if(this.fireEvent("beforeload", this, params) !== false) {
var s = [];
for (var x in params) {
s[s.length] = "params[\"" + x + "\"]";
}
s = s.join(",");
var o = {
params: params || {},
request: {
callback : callback,
scope : scope,
arg : arg
},
reader: reader,
callback: this.loadResponse,
scope: this
};

eval("this.ajaxProObject[this.method](" + s + ", this.loadResponse, o)");

} else {
callback.call(scope||this, null, arg, false);
}
},


// Rodiniz's response handler

loadResponse: function(response, request) {

var o = response.context;

var result;
try {
result = o.reader.read(response.json);

}catch(e){
this.fireEvent("loadexception", this, o, response, e);
o.request.callback.call(o.request.scope, null, o.request.arg, false);
return;
}
o.request.callback.call(o.request.scope, result, o.request.arg, true);
},


slight mod in Rodiniz's reader....
read : function(response){

var r= new Object(); //to handle errors
var obj=response + "*/";
var doc = eval(obj);
if(r.error){
throw r.error;
}
return this.readRecords(doc);
},

No comments: