PDA

View Full Version : Adding a converted value to a store



cnelissen
27 Apr 2010, 11:52 AM
When adding a record to a store, it seems the record does not get passed through any convert functions specified in the field definition. I need a way to convert values as they are added to the store. Here is a very simple example of the current behavior:


var store = new Ext.data.ArrayStore({
fields: [
{name: 'id'},
{name: 'intTest', type: 'int', convert: function(v){return (v === '1' || v === true ? 1 : 0); }}
],
data: [
['1', '1'],
['2', '0'],
['3', true],
['4', false]
]
});

var rec = new store.recordType({
'id': '5',
'intTest': true
});
store.add(rec);

Ext.each(store.data.items, function(obj) {
console.log('id: ' + obj.data.id + ' intTest: ' + obj.data.intTest);
});


In the console this gets returned as:


id: 1 intTest: 1
id: 2 intTest: 0
id: 3 intTest: 1
id: 4 intTest: 0
id: 5 intTest: true

Obviously the last record should be a 1 instead of true.

Any ideas on what I am missing, or how to go about getting the right value into the store?

tobiu
28 Apr 2010, 1:48 AM
hi clint,

the matching is designed to convert values of the data-input into records. since you specify a new record directly, there is no matching. the solution is quite easy though. specify the function outside the data-array, like:



function convertData(v){
return (v === '1' || v === true ? 1 : 0);
}

var store = new Ext.data.ArrayStore({
fields: [
{name: 'id'},
{name: 'intTest', type: 'int', convert: convertData(v)}
],
data: [
['1', '1'],
['2', '0'],
['3', true],
['4', false]
]
});

var rec = new store.recordType({
'id': '5',
'intTest': convertData(true)
});



untested and written quite fast. it could be, that you don't need to specify the param for the function.
if convert: convertData(v) does not work, try convert: convertData.


kind regards,
tobiu

cnelissen
28 Apr 2010, 7:27 AM
Thanks for the reply Tobiu, however my actual implementation is a bit more complex than the example I posted. I would of course just covert the data if it was from a more predictable source, however I am trying to build a class that will add different types of values to different stores, so I will not always know what type of input is going to be specified. It will actually look something like:



this.rec = new this.store.recordType(this.form.getForm().getValues());


In the case of checkboxes, getValues will return the value as true or false, and I need to store data as an int, either 0 or 1, for an eventual write operation to a database.