PDA

View Full Version : 4.1 Beta 2 - Json reader and createAccessor



nbourdeau
15 Feb 2012, 6:30 AM
Hi,

I'm trying to test my application in the Beta 2 and I have a question regarding the internals of the Json reader.

Previously, I overrided the method "createAccessor" for custom needs. Now the method is still there but there is the new one "createFieldAccessExpression" which has the same documentation but seems to be called elsewhere.

What is exactly the purpose of this new function and why not merge the two ?

mitchellsimoens
15 Feb 2012, 6:40 AM
It looks like createFieldAccessExpression is for mapping of fields to response fields.

nbourdeau
15 Feb 2012, 7:44 AM
Ok I see ... now a function is builded as a string to extract data. Want I want is a "NullSafe" json extractor.
For example, if mapping is user.description, I want to handle the fact that "user" can be null...

I don't know how to achieve this in the new format ... my old custom function was doing something like this :


var parts = expr.split('.');
return function(value) {
for (var i =0; i < parts.length; i++) {
if (!value)
return null;
value = value[parts[i]];
}
return value;
};

dongryphon
23 Feb 2012, 1:29 AM
Can you use a converter for the field?

http://dev.sencha.com/deploy/ext-4.0.7-gpl/docs/#!/api/Ext.data.Field-cfg-convert

dongryphon
23 Feb 2012, 1:33 AM
A converter, if it can achieve your goals, is much better than operating on the private methods behind the reader. Obviously, one has to do what one has to do... :)

lmalgras
2 May 2012, 8:04 AM
I've just had the same kind problem during my migration from 4.0.7 to 4.1.0

Here is the convert function that I wrote to address this behaviour :


var jsonNullSafe = function(field, prop) {
return function(v, record) {
var json = record.get(field) || {},
value = json[prop];
return value || v;
}
};

Ext.define('Book', {
extend: 'Ext.data.Model',
fields: [{
name: 'title'
}, {
name: 'author'
}, {
name: 'authorFirstName',
convert: jsonNullSafe('author', 'fn')
}, {
name: 'authorLastName',
convert: jsonNullSafe('author', 'ln')
}]
});


In 4.0.7 my model was :


Ext.define('Book', {
extend: 'Ext.data.Model',
fields: [{
name: 'title'
}, {
name: 'authorFirstName',
mapping: 'author.fn'
}, {
name: 'authorLastName',
mapping: 'author.ln'
}]
});