Arthur.Blake
28 Mar 2008, 9:01 AM
UPDATE 2008-04-22: Created new post in the plugins forum here: http://extjs.com/forum/showthread.php?t=33353
PLEASE POST YOUR COMMENTS/QUESTIONS THERE!
UPDATE 2008-04-21: Full demo and source code is now online here: http://jabsorb.org/jabsorb-trunk/projectmetrics.html
I've recently starting learning ExtJS. I must say, I'm very impressed. It's quite elegant, powerful and beautiful. The only issue I have is that it's a bit big... I spent a little time trying to make a smaller custom ExtJS build-- but it's still big! (mainly because I wanted to use a lot of the features!) That point aside, it's a great framework that I'll be spending a lot of time with in the upcoming days...
My first goal was to get the grid working with my favorite server side framework, jabsorb (http://jabsorb.org). After some extensive searching, I could not find anyone else using ExtJS with jabsorb (http://jabsorb.org) or json-rpc-java (http://oss.metaparadigm.com/jsonrpc/), so I coded it up!
jabsorb and json-rpc-java abstract away the whole server side interaction from you so you don't need to worry about URLs, XHR or even data serialization/transformation...
So, what I did was make a new kind of proxy, I based my code off of the HttpProxy, but it really just wraps a json-rpc call/callback.
This worked beautifully with the jabsorb framework and an ExtJS JsonReader. If anyone is interested, I will try and post a more complete example and demo with some server side code, later.
Here's the code:
/**
* @class Ext.data.JsonRpcProxy
* @extends Ext.data.DataProxy
*
* An implementation of Ext.data.DataProxy that invokes a json-rpc method call
* via the json-rpc-java and/or jabsorb library to retrieve the data needed by
* the Reader when it's load method is called.
*
* @constructor
* @param {Function} rpc The jabsorb or json-rpc-java function to call to
* retrieve the data object which the Reader uses to
* construct a block of Ext.data.Records. When invoked,
* it will be passed one json object containing the call
* parameters used to retrieve and optionally sort the data
* and a second callback function to load the resultant
* data from the result of the rpc call.
*/
Ext.data.JsonRpcProxy = function(rpc) {
Ext.data.JsonRpcProxy.superclass.constructor.call(this);
this.rpc = rpc;
};
Ext.extend(Ext.data.JsonRpcProxy, Ext.data.DataProxy, {
/**
* Load data from the requested source read the data object into
* a block of Ext.data.Records using the passed Ext.data.DataReader
* implementation and process that block using the passed callback.
*
* @param {Object} params This parameter is not used by the JsonRpcProxy class.
* @param {Ext.data.DataReader) reader The Reader object which converts the data
* object into a block of Ext.data.Records.
* @param {Function} callback The function into which to pass the block of Ext.data.records.
* The function must be passed <ul>
* <li>The Record block object</li>
* <li>The "arg" argument from the load function</li>
* <li>A boolean success indicator</li>
* </ul>
* @param {Object} scope The scope in which to call the callback
* @param {Object} arg An optional argument which is passed to the callback as its second parameter.
*/
load : function(params, reader, callback, scope, arg){
if(this.fireEvent("beforeload", this, params) !== false)
{
var o = {
params : params || {},
request: {
callback : callback,
scope : scope,
arg : arg
},
reader: reader,
callback : this.loadResponse,
scope: this
};
this.rpc(function(r,e){o.callback.call(o.scope,o,r&&!e, r||e);}, o.params);
}
else
{
callback.call(scope||this, null, arg, false);
}
},
// private
loadResponse : function(o, success, response){
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.readRecords(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);
},
// private
update : function(dataSet){
},
// private
updateResponse : function(dataSet){
}
});
This will also work perfectly for the older json-rpc-java server side framework which jabsorb is based on, and indeed should work with any back end json-rpc 1.0 compliant server, (using the jsonrpc.js front end call methodology from jabsorb (http://jabsorb.org) or json-rpc-java (http://oss.metaparadigm.com/jsonrpc/).)
I just wrote this as if it were part of ExtJS and indeed it would be great if it were in a future release if the code is up to the ExtJS team's standards. If there are problems or issues with it, please point that out to me (I'm still quite new to ExtJS) -- I'd like this to be a good clean reusable implementation...
PLEASE POST YOUR COMMENTS/QUESTIONS THERE!
UPDATE 2008-04-21: Full demo and source code is now online here: http://jabsorb.org/jabsorb-trunk/projectmetrics.html
I've recently starting learning ExtJS. I must say, I'm very impressed. It's quite elegant, powerful and beautiful. The only issue I have is that it's a bit big... I spent a little time trying to make a smaller custom ExtJS build-- but it's still big! (mainly because I wanted to use a lot of the features!) That point aside, it's a great framework that I'll be spending a lot of time with in the upcoming days...
My first goal was to get the grid working with my favorite server side framework, jabsorb (http://jabsorb.org). After some extensive searching, I could not find anyone else using ExtJS with jabsorb (http://jabsorb.org) or json-rpc-java (http://oss.metaparadigm.com/jsonrpc/), so I coded it up!
jabsorb and json-rpc-java abstract away the whole server side interaction from you so you don't need to worry about URLs, XHR or even data serialization/transformation...
So, what I did was make a new kind of proxy, I based my code off of the HttpProxy, but it really just wraps a json-rpc call/callback.
This worked beautifully with the jabsorb framework and an ExtJS JsonReader. If anyone is interested, I will try and post a more complete example and demo with some server side code, later.
Here's the code:
/**
* @class Ext.data.JsonRpcProxy
* @extends Ext.data.DataProxy
*
* An implementation of Ext.data.DataProxy that invokes a json-rpc method call
* via the json-rpc-java and/or jabsorb library to retrieve the data needed by
* the Reader when it's load method is called.
*
* @constructor
* @param {Function} rpc The jabsorb or json-rpc-java function to call to
* retrieve the data object which the Reader uses to
* construct a block of Ext.data.Records. When invoked,
* it will be passed one json object containing the call
* parameters used to retrieve and optionally sort the data
* and a second callback function to load the resultant
* data from the result of the rpc call.
*/
Ext.data.JsonRpcProxy = function(rpc) {
Ext.data.JsonRpcProxy.superclass.constructor.call(this);
this.rpc = rpc;
};
Ext.extend(Ext.data.JsonRpcProxy, Ext.data.DataProxy, {
/**
* Load data from the requested source read the data object into
* a block of Ext.data.Records using the passed Ext.data.DataReader
* implementation and process that block using the passed callback.
*
* @param {Object} params This parameter is not used by the JsonRpcProxy class.
* @param {Ext.data.DataReader) reader The Reader object which converts the data
* object into a block of Ext.data.Records.
* @param {Function} callback The function into which to pass the block of Ext.data.records.
* The function must be passed <ul>
* <li>The Record block object</li>
* <li>The "arg" argument from the load function</li>
* <li>A boolean success indicator</li>
* </ul>
* @param {Object} scope The scope in which to call the callback
* @param {Object} arg An optional argument which is passed to the callback as its second parameter.
*/
load : function(params, reader, callback, scope, arg){
if(this.fireEvent("beforeload", this, params) !== false)
{
var o = {
params : params || {},
request: {
callback : callback,
scope : scope,
arg : arg
},
reader: reader,
callback : this.loadResponse,
scope: this
};
this.rpc(function(r,e){o.callback.call(o.scope,o,r&&!e, r||e);}, o.params);
}
else
{
callback.call(scope||this, null, arg, false);
}
},
// private
loadResponse : function(o, success, response){
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.readRecords(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);
},
// private
update : function(dataSet){
},
// private
updateResponse : function(dataSet){
}
});
This will also work perfectly for the older json-rpc-java server side framework which jabsorb is based on, and indeed should work with any back end json-rpc 1.0 compliant server, (using the jsonrpc.js front end call methodology from jabsorb (http://jabsorb.org) or json-rpc-java (http://oss.metaparadigm.com/jsonrpc/).)
I just wrote this as if it were part of ExtJS and indeed it would be great if it were in a future release if the code is up to the ExtJS team's standards. If there are problems or issues with it, please point that out to me (I'm still quite new to ExtJS) -- I'd like this to be a good clean reusable implementation...