PDA

View Full Version : Ext.ux.data.JsonRpcProxy



cginzel
3 Jul 2010, 1:47 PM
I'm playing with NodeJS (http://github.com/ry/node) and Connect (http://github.com/extjs/Connect) which has an JSONRPC provider and so I managed to coble this proxy and reader together that I was able to successfully use to retrieve data into a grid. But I'm moving onto building an Ext.Direct provider instead so it'll be easier to build out the GUI. But for posterity's sake, I thought I'd post this code here just in case anyone else might find it useful.

I don't intend to support it, so use it at your own risk! :D

Update: Posted sample direct app running on Node w/ Connect and a MongoDB back-end here (http://www.sencha.com/forum/showthread.php?103315-Ext.Direct-provider-and-example-for-Connect-on-NodeJS).


Ext.namespace("Ext.ux.data");

Ext.ux.data.JsonRpcProxy = function(config)
{
this.jsonId = 0;
Ext.apply(this, config);
Ext.ux.data.JsonRpcProxy.superclass.constructor.call(this);
}

Ext.extend(Ext.ux.data.JsonRpcProxy, Ext.data.DataProxy, {

method: 'list',

getJsonId: function() {
return ++this.jsonId;
},

createJsonParams: function(method, methodParams) {
var args=[];
for (i=0; i<methodParams.length; i++){
args.push(methodParams[i]);
}
return {jsonrpc: '2.0', id: this.getJsonId(), method: method, params: args};
},

jsonLoadException: function(response) {
var respht = Ext.util.Format.trim(response.getResponseHeader("Content-Type")) ;
if (respht.indexOf('application/json') != -1) {
var o = Ext.util.JSON.decode(response.responseText);
if (o.error && o.error.message) {
Ext.Msg.alert("Service Error", o.error.message);
return;
}
}
Ext.Msg.alert('Service Error', 'Error while reading data.');
},

getConnection : function(){
return Ext.Ajax;
},

load : function(params, reader, callback, scope, arg){
if(this.fireEvent("beforeload", this, params) !== false){
var o = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
jsonData: this.createJsonParams(this.method, params),
request: {
callback : callback,
scope : scope,
arg : arg
},
reader: reader,
callback : this.loadResponse,
scope: this,
url: scope.url
};
if(this.activeRequest){
Ext.Ajax.abort(this.activeRequest);
}
this.activeRequest = Ext.Ajax.request(o);
}else{
callback.call(scope||this, null, arg, false);
}
},


loadResponse : function(o, success, response){
delete this.activeRequest;

if (success) {
var respht = Ext.util.Format.trim(response.getResponseHeader("Content-Type")) ;
if (respht.indexOf('application/json') == -1) {
success = false;
}
}

if(!success){
this.fireEvent("loadexception", this, o, response);
o.request.callback.call(o.request.scope, null, o.request.arg, false);
return;
}

var result;
try {
result = o.reader.read(response);
}catch(e){
this.fireEvent("loadexception", this, o, response, e);
o.request.callback.call(o.request.scope, null, o.request.arg, false);
return;
}
this.fireEvent("load", this, o, o.request.arg);
o.request.callback.call(o.request.scope, result, o.request.arg, true);
},
update : function(dataSet){},
updateResponse : function(dataSet){}
});

Ext.ux.data.JsonRpcReader = Ext.extend(Ext.data.JsonReader, {
read : function(response){
var json = response.responseText;
var o = Ext.decode(json);
if(!o) {
throw {message: 'JsonReader.read: Json object not found'};
} else if (o.jsonrpc !== '2.0') {
throw new Ext.data.JsonReader.Error('jsonrpc-wrong-version', o.jsonrpc);
} else {
o = o.result;
}

return this.readRecords(o);
},

readResponse : function(action, response) {
var o = (response.responseText !== undefined) ? Ext.decode(response.responseText) : response;
if(!o) {
throw new Ext.data.JsonReader.Error('response');
} else if (o.jsonrpc !== '2.0') {
throw new Ext.data.JsonReader.Error('jsonrpc-wrong-version', o.jsonrpc);
} else {
o = o.result;
}

var root = this.getRoot(o);
if (action === Ext.data.Api.actions.create) {
var def = Ext.isDefined(root);
if (def && Ext.isEmpty(root)) {
throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
}
else if (!def) {
throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
}
}

// instantiate response object
var res = new Ext.data.Response({
action: action,
success: this.getSuccess(o),
data: (root) ? this.extractData(root, false) : [],
message: this.getMessage(o),
raw: o
});

// blow up if no successProperty
if (Ext.isEmpty(res.success)) {
throw new Ext.data.JsonReader.Error('successProperty-response', this.meta.successProperty);
}
return res;
}
});