PDA

View Full Version : Store won't sync without setDirty



redmagic
31 Jul 2012, 2:51 AM
Hi,

I've got a Store, add models to it.

It won't sync because the store does not see the added records. In the add event listener, getUpdatedRecords() returns an empty array.

This is fixable by calling the model's setDirty() method before adding them. However, this is not documented so I would like to know if it's a mistake on my side or a bug.

Here we go.

The Model


Ext.define('SM.model.SchoolClass', {
extend: 'Ext.data.Model',


fields: ['id','name','students']


});


The Store


Ext.define('SM.store.SchoolClasses', {
extend: 'Ext.data.Store',
model: 'SM.model.SchoolClass',


proxy: {
type: 'ajax',
reader: {
type: 'json',
root: 'schoolClasses'
},
writer: {
type: 'json',
root: 'schoolClasses'
}
}


});


The controller sets the proxies url on init


this.store.getProxy().url = this.application.parameters.services.schoolclasses;





A record is added to the store


addServerClass: function (name) {


var newClass = Ext.create('SM.model.SchoolClass', {
id: 123,
name: name,
students: []
});


newClass.setDirty();


this.store.add(newClass);


this.store.sync({
callback: function () {
console.log('synced',arguments)
}
});
},




Without the call to setDirty(), the store's sync method won't trigger a proxy write.

Please let me know!


Kind regards,

Arjen Geerse

Farish
31 Jul 2012, 2:59 AM
try adding the record without id. that way, it should be marked as a new record and store.getNewRecords() should return this record. the proxy will send the request for "create".

redmagic
31 Jul 2012, 3:05 AM
Hi,

Thanks for your fast reply!
Leaving the id out worked.

For me, it was not entirely clear from the documentation though.

Regards,

Arjen