crp_spaeth
11 May 2009, 8:10 AM
Hi there,
after reading the code of RemoteProvider is stuck at the doCall Method and want to share a few thoughts about the len Parameter.
For those who didn't dive into the code yet the current docall Method:
doCall : function(c, m, args){
var data = null, hs = args[m.len], scope = args[m.len+1];
if(m.len !== 0){
data = args.slice(0, m.len);
}
var t = new Ext.Direct.Transaction({
provider: this,
args: args,
action: c,
method: m.name,
data: data,
cb: scope && typeof hs == 'function' ? hs.createDelegate(scope) : hs
});
if(this.fireEvent('beforecall', this, t) !== false){
Ext.Direct.addTransaction(t);
this.queueTransaction(t);
this.fireEvent('call', this, t);
}
},
What I dont like about the current implementation, that you need to have a length propertie for each RF. While this is useful for many cases there are many cases where some args are optional for example. Okay I could add a null to avoid running into exception but I think it would be much nicer to have something like a minlen, maxlen and the possibility to even not set len at all.
I implemented that and it could look something like that:
doCall : function(c, m, args){
var data = null;
var minLen = m.minLen ? m.minLen : m.len;
var maxLen = m.maxLen ? m.maxLen : m.len;
var argsLen = args.length;
// check last an prelast for function
var cb;
if(argsLen >= 2 && typeof args[argsLen-2] == 'function'){
cb = args[argsLen-2].createDelegate(args[argsLen-1]);
argsLen = argsLen -2;
} else if(argsLen >= 1 && typeof args[argsLen-1] == 'function') {
cb = args[argsLen-2];
argsLen = argsLen -1;
}
if(maxLen && argsLen > maxLen) {
throw("The method: " + m.name + " from action: " + c +" was called with to many Arguments! Maxum Arguments: "+ maxLen+ " Sent arguments: "+argsLen);
}
if(minLen && argsLen > minLen) {
throw("The method: " + m.name + " from action: " + c +" was called with to less Arguments! Minimal Arguments: "+ minLen+ " Sent arguments: "+argsLen);
}
if(argsLen !== 0){
data = args.slice(0, argsLen);
}
var t = new Ext.Direct.Transaction({
provider: this,
args: args,
action: c,
method: m.name,
data: data,
cb: cb
});
if(this.fireEvent('beforecall', this, t) !== false){
Ext.Direct.addTransaction(t);
this.queueTransaction(t);
this.fireEvent('call', this, t);
}
}
What do you think?
after reading the code of RemoteProvider is stuck at the doCall Method and want to share a few thoughts about the len Parameter.
For those who didn't dive into the code yet the current docall Method:
doCall : function(c, m, args){
var data = null, hs = args[m.len], scope = args[m.len+1];
if(m.len !== 0){
data = args.slice(0, m.len);
}
var t = new Ext.Direct.Transaction({
provider: this,
args: args,
action: c,
method: m.name,
data: data,
cb: scope && typeof hs == 'function' ? hs.createDelegate(scope) : hs
});
if(this.fireEvent('beforecall', this, t) !== false){
Ext.Direct.addTransaction(t);
this.queueTransaction(t);
this.fireEvent('call', this, t);
}
},
What I dont like about the current implementation, that you need to have a length propertie for each RF. While this is useful for many cases there are many cases where some args are optional for example. Okay I could add a null to avoid running into exception but I think it would be much nicer to have something like a minlen, maxlen and the possibility to even not set len at all.
I implemented that and it could look something like that:
doCall : function(c, m, args){
var data = null;
var minLen = m.minLen ? m.minLen : m.len;
var maxLen = m.maxLen ? m.maxLen : m.len;
var argsLen = args.length;
// check last an prelast for function
var cb;
if(argsLen >= 2 && typeof args[argsLen-2] == 'function'){
cb = args[argsLen-2].createDelegate(args[argsLen-1]);
argsLen = argsLen -2;
} else if(argsLen >= 1 && typeof args[argsLen-1] == 'function') {
cb = args[argsLen-2];
argsLen = argsLen -1;
}
if(maxLen && argsLen > maxLen) {
throw("The method: " + m.name + " from action: " + c +" was called with to many Arguments! Maxum Arguments: "+ maxLen+ " Sent arguments: "+argsLen);
}
if(minLen && argsLen > minLen) {
throw("The method: " + m.name + " from action: " + c +" was called with to less Arguments! Minimal Arguments: "+ minLen+ " Sent arguments: "+argsLen);
}
if(argsLen !== 0){
data = args.slice(0, argsLen);
}
var t = new Ext.Direct.Transaction({
provider: this,
args: args,
action: c,
method: m.name,
data: data,
cb: cb
});
if(this.fireEvent('beforecall', this, t) !== false){
Ext.Direct.addTransaction(t);
this.queueTransaction(t);
this.fireEvent('call', this, t);
}
}
What do you think?