View Full Version : JsonReader/Record mapping aggregating multiple fields
Animal
4 Jun 2007, 4:17 AM
Jack, on another thread, I've been musing that perhaps the accessor function should just surround the mapping expression with
with (obj) return <mapping expression>;
Then, you'd be able to just use any property names from the row object with no adding of "obj."
How about the following for JsonReader's accessor generator?
/**
* @ignore
*/
getJsonAccessor: function(){
return function(expr) {
var varNameRe = /^\w[\w_]$/;
try {
return(expr.match(varNameRe))
? function(rec){return rec[expr];}
: new Function("rec", "with(rec){return " + expr + "};");
} catch(e){}
return Ext.emptyFn;
};
}(),
So you could just use eg:
mapping: 'firstName + " " + secondName'
To pull data from several different fields of the row object.
jack.slocum
4 Jun 2007, 2:32 PM
Looks promising. Have you tested it for performance? I'd be interested in knowing the results.
Animal
4 Jun 2007, 11:31 PM
Subjectively, paging the grid it seems perhaps faster. Profiling in Firebug can't even catch any CPU time allocated to the function - it say 0ms.
jack.slocum
5 Jun 2007, 1:14 AM
Great! Can I ask what the try/catch is for? It looks like it catching malformed json specs, but wouldn't it make more sense to let those errors go through or throw a nice message?
Animal
5 Jun 2007, 2:09 AM
Yes, it is to catch malformed mappings.
The exception is thrown at the time the accessor function is compiled. That is, on read of the first record (or first record after metadata change)
This works. It shows an explanatory message in each cell of the grid that uses that mapping:
/**
* @ignore
*/
getJsonAccessor: function(){
return function(expr) {
var varNameRe = /^\w[\w_]$/;
try {
return(expr.match(varNameRe))
? function(rec){return rec[expr];}
: new Function("rec", "with(rec){return " + expr + "};");
} catch(e){
return function(){return e.message + ' in mapping "' + expr + '"'};
}
};
}(),
Animal
5 Jun 2007, 2:12 AM
Maybe it should just be even simpler and just use the with() method to always catch these errors:
/**
* @ignore
*/
getJsonAccessor: function(expr) {
try {
return new Function("rec", "with(rec){return " + expr + "};");
} catch(e){
return function(){return e.message + ' in mapping "' + expr + '"'};
}
},
jack.slocum
5 Jun 2007, 4:17 AM
Hmmm... I am liking that last one. Looks nice. One question though, what about a mapping like "col-data". That would bomb right?
Animal
5 Jun 2007, 4:21 AM
Only if either of those properties didn't exist!
If the row object looked like this:
{col: 4, data: 2}
Then it would work fine.
Just paste this into the Firebug console:
var t={col: 4, data: 2};
with(t){col-data};
jack.slocum
5 Jun 2007, 4:42 AM
Yes, I mean the field is called 'col-data'. ;) Like this is valid JSON:
{"foo": "bar", "col-data": 123}
Animal
5 Jun 2007, 5:35 AM
Hmm. Using quotes to get round variable naming rules.
I can't see a justification for doing this, but you could get round it be reintroducing a regexp test to see if
expr.match(/^(['"]).*?\1$/)
And use the simple array notation accessor if it matches.
Animal
5 Jun 2007, 5:38 AM
// private
quotedStr: /^(['"]).*\1$/,
/**
* @ignore
*/
getJsonAccessor: function(expr) {
try {
return(expr.match(this.quotedStr))
? function(rec){return rec[expr];}
: new Function("rec", "with(rec){return " + expr + ";}");
} catch(e){
return function(){return e.message + ' in mapping "' + expr + '"'};
}
},
manugoel2003
5 Jun 2007, 8:33 AM
ok, this thread went right over my head :D .... u ppl are amazing... I feel so obsolete
I need this feature & patched my 2.2 ext-all-debug.js thusly:
quotedStr: /^(['"]).*\1$/,
/**
* @ignore
*/
getJsonAccessor: function(expr) {
var re = /[\[\.]/;
try {
if (expr.match(this.quotedStr)) {
return new Function("rec", "with(rec){return " + expr + ";}");
} else {
if (re.test(expr)) {
return new Function("obj", "return obj." + expr);
} else {
return function(obj){ return obj[expr]; };
}
}
} catch(e){
return function(){return e.message + ' in mapping "' + expr + '"'};
}
},
Seems to work pretty well... :D
Animal
17 Dec 2008, 12:22 AM
Don't patch the main file. This is what Ext.override is for.
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.