-
5 Feb 2013 1:05 PM #1
Answered: model.convert() not called when accessing field via model.get()
Answered: model.convert() not called when accessing field via model.get()
I have the following model:
When I call do the following, the convert() function is not called for my model instance:Code:(function() { var fourDecimals = function(value, record) { return parseFloat(value.toFixed(4)); } Ext.define('App.model.Stock', { extend: 'Ext.data.Model', fields: [ {name: 'Symbol', type: 'string'}, {name: 'TradePrice', type: 'float'}, {name: 'Bid', type: 'float'}, {name: 'Ask', type: 'float'}, {name: 'Volume', type: 'int'}, {name: 'TodaysClose', type: 'float'}, {name: 'PreviousClose', type: 'float'}, { name: 'PriceChange', type: 'float', convert: function(value, record) { return parseFloat((record.get('TradePrice') - record.get('PreviousClose')).toFixed(4)); } },{ name: 'PercentPriceChange', type: 'float', convert: function(value, record) { return parseFloat((((record.get('TradePrice') / record.get('PreviousClose')) - 1) * 100).toFixed(2)); } } ] }); })();
Is this a bug?Code:model.set('TradePrice', 12.34); var priceChange = model.get('PriceChange');
-
Best Answer Posted by chaddjohnson
This does work in Sencha Touch 2. When a field that a convert() function depends on is updated, the convert() function is called.
I found a workaround based on this blog post:
I added that to the model.Code:set: function(fieldName, newValue) { this.callParent(arguments); // Force recalculation of dependent fields. if (typeof fieldName == 'object' || fieldName == 'TradePrice' || fieldName == 'PreviousClose') { this.set('PriceChange'); this.set('PercentPriceChange'); } }
-
7 Feb 2013 11:32 AM #2
This does work in Sencha Touch 2. When a field that a convert() function depends on is updated, the convert() function is called.
I found a workaround based on this blog post:
I added that to the model.Code:set: function(fieldName, newValue) { this.callParent(arguments); // Force recalculation of dependent fields. if (typeof fieldName == 'object' || fieldName == 'TradePrice' || fieldName == 'PreviousClose') { this.set('PriceChange'); this.set('PercentPriceChange'); } }


Reply With Quote