PDA

View Full Version : Ext Direct



corvonero
12 Sep 2010, 5:38 PM
This is a VERY VERY dirty hack to enable Touch to work with Ext-Direct server side components.
This DOES NOT handle Direct clientside!
Only one query at a time is allowed.



Ext.data.DirectReader = Ext.extend(Ext.data.JsonReader, {
resultSet: 'result',
readRecords: function(data) {
data = data[0][this.resultSet];
return Ext.data.DirectReader.superclass.readRecords.call(this, data);
}
});

Ext.data.ReaderMgr.registerType('direct', Ext.data.DirectReader);



Ext.data.DirectProxy = Ext.extend(Ext.data.AjaxProxy, {
constructor: function(){
this.jsonData = arguments[0].jsonData;
Ext.data.DirectProxy.superclass.constructor.apply(this, arguments);
},
doRequest: function(operation, callback, scope) {
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);

if(operation.allowWrite()){
request = writer.write(request);
}

Ext.apply(request, {
headers : this.headers,
timeout : this.timeout,
scope : this,
callback: this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request)
});


request.jsonData = this.jsonData;
request.method = "POST";
Ext.Ajax.request(request);
return request;
}
});

Ext.data.ProxyMgr.registerType('direct', Ext.data.DirectProxy);



Then use as per this example:



var myStore =new Ext.data.JsonStore({
proxy: {
jsonData: {
type: 'rpc',
action: 'YOURCLASS',
method: 'YOURMETHOD',
tid: transid++,
data: [{
limit: 0,
query: '',
start: 0
}]
},
type: 'direct',
url: 'router.php',
method: 'POST',

reader: {
type: 'direct',
root: '',
idProperty: 'id'
}
},

fields: ['field1', 'field2']
});



...just a quick way to not throw all the server side work into the bin!

M.

jay@moduscreate.com
13 Sep 2010, 5:52 AM
Moved To Examples

corvonero
14 Sep 2010, 12:45 AM
Hello Garcia,
thanks for the move...

here's a second version, which also accepts parameters...


Ext.data.DirectProxy = Ext.extend(Ext.data.AjaxProxy, {
constructor: function(){
this.jsonData = arguments[0].jsonData;
this.paramOrder = arguments[0].paramOrder;
Ext.data.DirectProxy.superclass.constructor.apply(this, arguments);
},
doRequest: function(operation, callback, scope) {
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);

if(operation.allowWrite()){
request = writer.write(request);
}

Ext.apply(request, {
headers : this.headers,
timeout : this.timeout,
scope : this,
callback: this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request)
});

console.log(request);

request.jsonData = this.jsonData;
if (this.paramOrder) {
for (var i = 0; i < this.paramOrder.length; i++) {
k = this.paramOrder[i]
request.jsonData.data[k] = request.params[k];
}
}
request.method = "POST";
Ext.Ajax.request(request);
return request;
}
});

Ext.data.ProxyMgr.registerType('direct', Ext.data.DirectProxy);

corvonero
21 Sep 2010, 5:31 PM
here you go with a MUCH better working code.
Please, be aware that this DOES NOT batch!
Batching should be quite easy to implement, but I should read all the original code, and I am too lazy atm.

Anyway, here you are:

DirectReader

Ext.data.DirectReader = Ext.extend(Ext.data.JsonReader, {
resultSet: 'result',
readRecords: function(data) {
data = data[0][this.resultSet];
return Ext.data.DirectReader.superclass.readRecords.call(this, data);
}
});

Ext.data.ReaderMgr.registerType('direct', Ext.data.DirectReader);

DirectProxy

Ext.data.DirectProxy = Ext.extend(Ext.data.AjaxProxy, {
constructor: function(){
Ext.data.DirectProxy.superclass.constructor.apply(this, arguments);
},
doRequest: function(operation, callback, scope) {


this.url = operation.url;
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);

if(operation.allowWrite()){
request = writer.write(request);
}

Ext.apply(request, {
headers : this.headers,
timeout : this.timeout,
scope : this,
callback: this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request)
});

request.jsonData = operation.jsonData;
if (operation.paramOrder) {
request.jsonData.data = [];
for (var i = 0; i < operation.paramOrder.length; i++) {
k = operation.paramOrder[i]
request.jsonData.data[i] = operation.params[k];
}
}
request.method = "POST";
Ext.Ajax.request(request);
return request;
}
});

Ext.data.ProxyMgr.registerType('direct', Ext.data.DirectProxy);


DirectStore


Ext.data.DirectStore = Ext.extend(Ext.data.Store, {
transId : 0,
constructor: function(config) {
config = config || {};
Ext.applyIf(config, {
proxy: {
type : 'direct',
reader: 'direct',
writer: 'json'
}
});
Ext.data.DirectStore.superclass.constructor.call(this, config);

},
read: function(options) {
options = options || {};
this.transId++;
Ext.applyIf(options, {
group : {
field: this.groupField,
direction: this.groupDir
},
url : this.url,
limit : this.pageSize,
addRecords: false,
params : this.params,
paramOrder: this.paramOrder,
jsonData : {
type: 'rpc',
action: this.directAction,
method: this.directMethod,
tid: this.transId,
data: []
}
});
return Ext.data.Store.superclass.read.call(this, options);
}
});
Ext.reg('directstore', Ext.data.DirectStore);

Example usage:




store = new Ext.data.DirectStore({

transId: 1,
url: "router.php",
directAction: 'myPHPClass',
directMethod: 'myPHPMethod',
paramOrder: ['param_one','param_two','param_three'],
params: {
param_one: 'none',
param_two: false,
param_three: true
},
fields: ['value', 'name', 'id']
});


store.params.param_one = 'all'; // instead of "none"
store.load();