PDA

View Full Version : convert 3.4 override to 4.1 override



ttbgwt
22 Aug 2012, 10:00 AM
Can someone please help with converting this 3.4 override over to a 4.1 override? Thanks!



Ext.override(Ext.data.JsonReader, {
getNestedJsonValue: function(obj, key) {
return eval('obj.' + key);
},

extractValues : function(data, items, len) {
var f, values = {};
for(var j = 0; j < len; j++){
f = items[j];
var v = this.ef[j](data);

//OVERRIDE
if (typeof f.convert !== 'function') {
values[f.name] = this.getNestedJsonValue((v !== undefined) ? v : f.defaultValue, f.convert);
}
else {
values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
}
}
return values;
}
});

ttbgwt
22 Aug 2012, 10:20 AM
I tried doing something like below, but got this firebug error when I ran:


TypeError: me.recordDataExtractorTemplate.apply is not a function at line 59686

Is it not possible to override a template?



Ext.define('MDC.patch.DataReaderReader', {
override: 'Ext.data.reader.Reader',


getNestedJsonValue: function (obj, key) {
return eval('obj.' + key);
},


recordDataExtractorTemplate : [
'var me = this\n',
' ,fields = me.model.prototype.fields\n',
' ,value\n',
' ,internalId\n',
'<tpl for="fields">',
' ,__field{#} = fields.get("{name}")\n',
'</tpl>', ';\n',


'return function(dest, source, record) {\n',
'<tpl for="fields">',
// createFieldAccessExpression must be implemented in subclasses to extract data from the source object in the correct way
' value = {[ this.createFieldAccessExpression(values, "__field" + xindex, "source") ]};\n',


// Code for processing a source property when a custom convert is defined
'<tpl if="hasCustomConvert">',
' if (typeof f.convert !== \'function\') {\n',
' dest["{name}"] = value === undefined ? this.getNestedJsonValue(__field{#}.defaultValue, __field{#}.convert) : this.getNestedJsonValue(value, __field{#}.convert);\n',
' else {\n',
' dest["{name}"] = value === undefined ? __field{#}.convert(__field{#}.defaultValue, record) : __field{#}.convert(value, record);\n',
' };\n',
// Code for processing a source property when there is a default value
'<tpl elseif="defaultValue !== undefined">',
' if (value === undefined) {\n',
' if (me.applyDefaults) {\n',
'<tpl if="convert">',
' dest["{name}"] = __field{#}.convert(__field{#}.defaultValue, record);\n',
'<tpl else>',
' dest["{name}"] = __field{#}.defaultValue\n',
'</tpl>',
' };\n',
' } else {\n',
'<tpl if="convert">',
' dest["{name}"] = __field{#}.convert(value, record);\n',
'<tpl else>',
' dest["{name}"] = value;\n',
'</tpl>',
' };',


// Code for processing a source property value when there is no default value
'<tpl else>',
' if (value !== undefined) {\n',
'<tpl if="convert">',
' dest["{name}"] = __field{#}.convert(value, record);\n',
'<tpl else>',
' dest["{name}"] = value;\n',
'</tpl>',
' }\n',
'</tpl>',


'</tpl>',


// set the client id as the internalId of the record.
// clientId handles the case where a client side record did not previously exist on the server,
// so the server is passing back a client id that can be used to pair the server side record up with the client record
'<tpl if="clientIdProp">',
' if (record && (internalId = {[ this.createFieldAccessExpression(\{mapping: values.clientIdProp\}, null, "source") ]})) {\n',
' record.{["internalId"]} = internalId;\n',
' }\n',
'</tpl>',


'};'
]


});

mankz
22 Aug 2012, 12:52 PM
Hmm, I've only ever used overrides to override methods. Looks like a quite big override of that string, should be some other way to achieve what you need. What is the purpose of your override?