Reputation: 20224
I have the requirement to recalculate a field in a model on a certain event. I want to calculate the value directly in the model, and not in the view renderers, because otherwise multiple views would also need custom sorting and grouping functions, so the code would bloat massively. I could however use convert
instead of calculate
, if that helps.
The fields on the model:
fields: [{
name: 'name',
type: 'string'
},{
name: 'localizedName',
calculate: function(data) {
return Globals.localize(data.name);
}
}]
and the function on the store:
onUILocaleChange: function() {
this.each(function(record) {
// force recalculate
});
}
I have made a fiddle that exhibits the behaviour I wish to achieve, but without the bad hacky workaround: https://fiddle.sencha.com/#view/editor&fiddle/2g7g
I would like this to work without line 73-75, in-place in the model.
Upvotes: 2
Views: 420
Reputation: 1439
Just alter your calculate function to send the Globals.language as a parameter:
{
name: 'localizedName',
persist: false,
calculate: function(data) {
return Globals.localize(data.name,Globals.language);
}
}
And then alter your localize function to receive that parameter:
localize: function(v,l) {
return Globals[l][v];
}
And last, just reload the store after changing the Globals.language on button event handler:
handler: function(btn) {
Globals.language = 'de';
btn.up('grid').getStore().load();
}
Delete "onUILocaleChange" entirely.
Upvotes: 1