PDA

View Full Version : Store.sync() doesn't work when Model applied but works when not there



tangix
19 Sep 2012, 1:31 AM
Maybe just a stupid mistake here, but I don't get why it doesn't work. I have a couple of stores in my small Sencha Architect project, configured as AjaxProxy with Reader, Writer and the fields configured inside of the Store. They all work - sync() sends changes to the server.

One Store is having 20+ fields so I thought I should break that out of the Store for tidyness and also to get a SequentialIdGenerator in place. Did that, set the Model of the Store and left the Reader/Writer on the Store. Adding records to the Store and doing sync() no longer works!

Moving the fields into the Store and all works well so the server and browser seems to be happy with the rest of the configuration.

For the moment I have moved the fields back into the Store and removed the SequentialIdGenerator (btw the Model approach did not work without this either) functionality and added that logic to the server, forcing me to reload the whole store from the server to get in sync... :-(


Ext.define('EL.model.QuestionModel', {
extend: 'Ext.data.Model',
alias: 'model.questionmodel',

fields: [
{
name: 'questiontext',
type: 'string'
},
{
name: 'questionid2',
type: 'int'
},
{
name: 'type',
type: 'int'
},
{
name: 'chkbox_count',
type: 'int'
},
{
name: 'value_unit',
type: 'string'
},
{
name: 'category',
type: 'string'
},
{
name: 'family',
type: 'string'
},
{
name: 'topic',
type: 'string'
},
{
name: 'ans1',
type: 'string'
},
{
name: 'ans2',
type: 'string'
},
{
name: 'ans3',
type: 'string'
},
{
name: 'ans4',
type: 'string'
},
{
name: 'ans5',
type: 'string'
},
{
name: 'ans6',
type: 'string'
},
{
name: 'ans7',
type: 'string'
},
{
name: 'ans8',
type: 'string'
},
{
name: 'corr_value1',
type: 'float'
},
{
name: 'corr_value2',
type: 'float'
},
{
name: 'corr_value3',
type: 'float'
},
{
name: 'corr_dev',
type: 'float'
},
{
name: 'corr_ans',
type: 'int'
},
{
name: 'questionorder',
type: 'int'
},
{
name: 'attachment',
type: 'string'
},
{
name: 'pic1',
type: 'string'
},
{
name: 'pic2',
type: 'string'
},
{
name: 'pic3',
type: 'string'
},
{
name: 'pic4',
type: 'string'
},
{
name: 'pic5',
type: 'string'
}
],

idgen: {
type: 'sequential',
id: 'questionid'
}
});


Ext.define('EL.store.QuestionStore', {
extend: 'Ext.data.Store',
alias: 'store.questionstore',

requires: [
'EL.model.QuestionModel'
],

constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
storeId: 'QuestionStore',
model: 'EL.model.QuestionModel',
proxy: {
type: 'ajax',
reader: {
type: 'json',
root: 'root'
},
writer: {
type: 'json',
allowSingle: false
}
}
}, cfg)]);
}
});

API setting code in launch (EL.config is set in a JS generated on the fly from the server - all other stores are configured in the same way):

Ext.getStore('QuestionStore').getProxy().url = EL.config.baseurl + 'question';
Ext.getStore('QuestionStore').getProxy().api = {
create : EL.config.baseurl + 'question/create',
read : EL.config.baseurl + 'question/read',
update : EL.config.baseurl + 'question/update',
destroy : EL.config.baseurl + 'question/destroy'
};


Any help greatly appreciated!

Thanks,
/Mattias

tangix
19 Sep 2012, 1:43 AM
BTW - the grid connected to the Store is updated after add() and insert() has been made, just no communication back to the server.

tangix
19 Sep 2012, 1:56 AM
Added more work-arounds to the SequentialID problem. Before adding, the server is now queried for the next id and it is inserted on the record added to the store. No need to reload the Store completely - yey!

vietits
19 Sep 2012, 2:09 AM
In version of using model, each time you add new record to store a new id will be generated and assigned to record. So this record is no longer phantom then it does not need to sync to server. If you don't use id generation , then sync() will do as normal.

In version of not using model, added record to store will have no id then it will be marked phantom and need to sync to server to create.

However, in both cases, if you modified or delete one or more existing records, sync() will do normally.

tangix
19 Sep 2012, 2:15 AM
You really got me to the next level of confusion here...
So if I use the Model with SeqID creation, the Store won't sync the record back to the server (shouldn't it?!?). Should I manually mark it dirty to get it to sync or am I using the things wrong?
I'll go back to the Model approach and remove the SeqID and return the record from the server as indicated in the Writer example.

Thanks,
/Mattias

scottmartin
19 Sep 2012, 4:46 AM
If you change the ID manually it will treated as an update instead of an insert, but will still be updated. If you want it treated as an insert, do not set the id field that affect phantom status.

In most all cases, the id created should be handled at the server.

Scott.

vietits
19 Sep 2012, 5:09 AM
Below is a solution if you still want to generate record Id at client side and then sync with server:


Ext.define('EL.model.QuestionModel', {
extend: 'Ext.data.Model',
alias: 'model.questionmodel',


fields: [
...
],


idgen: {
type: 'sequential',
id: 'questionid'
},
setId: function(id) {
this.set(this.idProperty, id);
this.phantom = true;
}
});