PDA

View Full Version : Suggestion: Add convertParams to field's convert function



wexman
27 Oct 2010, 4:23 AM
Hi,

I'm in a situation where I need to dynamically create a store, depending on the input data. This is working fine so far, but I'd like to make a suggestion which would make it easier for me (and I guess for others as well). Some of the fields in my record have a convert function which actually provides the data to store in the record. I would like to be able to pass a custom convertParams parameter to the convert function (because I have n fields that could all use the same function, but I don't want to have n functions because they're all doing the same. An example:


var myConvert = function(v, record, params) {
if (params.foo == 'bar')
return record.field1;
else
return record.field2;
}

var fields = [
{name: "field1", type: "int", convert: myConvert, convertParams: {foo: "bar"}},
{name: "field2", type: "string", convert: myConvert, convertParams: {foo: "doh"}},
];
I guess you get the idea....

What do you think?

wexman
27 Oct 2010, 5:59 AM
Never mind, found out that this is actually easy to achieve. I've overriden the extractValues method of the JsonReader like this:


Ext.override(Ext.data.JsonReader, {
extractValues: function (data, items, len) {
var f, values = {};
for (var j = 0; j < len; j++) {
f = items[j];
var v = this.ef[j](data);
values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data, f.convertParams !== undefined ? f.convertParams : null);
}
return values;
}
});May have been obvious, but this is the first time that I'm doing something like this. Next, I'll try to derive my own classes from JsonStore / JsonReader and put it there.

Thanks for listening ;-)

Animal
27 Oct 2010, 7:19 AM
var myConvert = function(v, record, params) {
if (params.foo == 'bar')
return record.field1;
else
return record.field2;
}

var fields = [
{name: "field1", type: "int", convert: myConvert.createDelegate(null, [{foo: "bar"}], true)},
{name: "field2", type: "string", convert: myConvert.createDelegate(null, [{foo: "doh"}], true)}
];

wexman
28 Oct 2010, 12:10 AM
Hi Animal,

thanks - that's even better!

Seems a little strange at a first glance, but it's working just fine!