jeudi 26 juin 2014

Force a Store/Model to re-evaluate field mappings when record udpated


Code:



Ext.define( 'MyApp.model.Message', {
extend: 'Ext.data.Model',
proxy : {
...
},
fields: [
'id', 'name', 'type', 'message'
{name: 'messageId', mapping: function( r ){
return (r.message && r.message.id ) ? r.message.id : ''
}},
{name: 'messageType', mapping: function( r ){
return (r.message && r.message.type ) ? r.message.type : ''
}},
{name: 'messageContent', mapping: function( r ){
return (r.message && r.message.content ) ? r.message.content : ''
}}
]
} );

Ext.define( 'MyApp.store.Messages', {
extend : 'Ext.data.Store',
model : 'MyApp.model.Message',
requires : 'MyApp.model.Message',
autoSync : true,
autoLoad : false,
remoteSort : true,
remoteFilter: true,
pageSize : 50,
sorters : {property: 'date', direction: 'ASC'}
} );

Ext.define( 'MyApp.view.Messages', {
extend : 'Ext.view.View',
...
initComponent: function(){

var me = this;

me.initStores();

Ext.apply( me, {
id : Ext.id(),
listeners: {
el: {
click : me.onActionClick,
delegate: '[data-event]',
scope : me
}
}
} );

me.store.load();
me.callParent( arguments );

},

onActionClick: function( evt, el, opts ){
var rec = this.getSelectionModel().getSelection()[0];

rec.beginEdit();
rec.set( {
messsage : {
id : 3,
type : 'otherType',
content : 'new content'
}
} );
rec.endEdit();
rec.commit();
}
} );

This is obviously a very hacked together example from our code which I've taken a ton of stuff out but the general question is this:

Why, when we update a field value in a record, does it not re-evaluate the mapping functions? Is there a way to force it to? Alternatively do we have to resort to using the "last minute" prepareData function (where it's available) to do this mapping on the fly (as this does get called in data views when a record gets updated).


Here's a quick use case of why this is important (at least to us );


A user action, a click for example, triggers an ajax call that tells the server to update the status of a field from 'field.status.draft' to 'field.status.published'. The response to the ajax method contains the updated record so we can integrate that back into our store without having to make another server round trip to refresh our store. All great so far but.....the mapping functions aren't called again on the new record values so any computed fields are not correct. So, back to my questions; is there a way to force the mapping functions to kick in or failing that, is there a better standard practice we should implement?


Thanks all!





Aucun commentaire:

Enregistrer un commentaire