PDA

View Full Version : [FIXED-EXTJSIV-212] Ext.data.Store.sync() does not update 'id' of phantom records



madrabaz
3 Mar 2011, 12:25 PM
Ext.data.Store.Sync() does not update 'id' of phantom records which are pushed the store by
Ext.data.Store.add() or Ext.data.Store.insert(). By the way, Ext.data.Model.save() works naturally.

After 'Add' and 'Sync Store'
http://www.sencha.com/forum/attachment.php?attachmentid=24960&d=1299183914
After 'Add' and 'Save Model'
http://www.sencha.com/forum/attachment.php?attachmentid=24961&d=1299183915

Test Script


Ext.require('Ext.tip.QuickTips');

Ext.onReady(function () {
Ext.QuickTips.init();

Ext.regModel('Employee', {
fields: ['id', 'Name', 'Surname', 'Salary', 'Bonus'],
proxy: {
type: 'rest',
reader: {
type: 'json',
root: 'data',
totalProperty: 'total',
successProperty: 'success',
messageProperty: 'message'
},
url: 'http://localhost/ExtStoreTest/Employee/'
}
});

new Ext.data.Store({
storeId: 'employeeStore',
model: 'Employee',
autoLoad: true
});

var newEmp;

var gridForm = Ext.create('Ext.grid.GridPanel', {
id: 'employee-grid',
title: 'Emplyees',
store: 'employeeStore',
headers: [{
text: 'ID',
width: 25,
sortable: false,
dataIndex: 'id'
},
{
text: 'Name',
width: 75,
sortable: true,
dataIndex: 'Name'
},
{
text: 'Surname',
flex: 1,
sortable: true,
dataIndex: 'Surname'
},
{
text: 'Salary',
width: 50,
sortable: true,
dataIndex: 'Salary'
},
{
text: 'Bonus',
width: 50,
sortable: true,
dataIndex: 'Bonus'
}],
height: 250,
width: 400,
columnLines: true,
frame: true,
margin: '5px',
tbar: [{
text: 'Add',
handler: function () {
var Emp = Ext.ModelMgr.getModel('Employee');

newEmp = new Emp({
Name: 'Conan', Surname: 'Barbar', Salary: 1000, Bonus: 100
});

Ext.data.StoreMgr.get('employeeStore').add(newEmp);

}
}, {
text: 'Update',
handler: function () {
var emp = Ext.data.StoreMgr.get('employeeStore').getAt(0);
emp.set('Salary', 5000);
}
}, {
text: 'Delete',
handler: function () {
Ext.data.StoreMgr.get('employeeStore').removeAt(1);
}
}, '->', {
text: 'Log Store',
handler: function () {
Ext.data.StoreMgr.get('employeeStore').each(function (r) {
console.log(r.getId(), r.dirty, r.phantom)
});
}
}, {
text: 'Sync Store',
handler: function () {
Ext.data.StoreMgr.get('employeeStore').sync();
}
}, {
text: 'Save Model',
handler: function () {
newEmp.save();
}
}],
renderTo: document.body
});
});


OS : Windows 7 Pro SP1
Browser: Chrome 10.0
Ext: 4.0PR5

icflorescu
3 Mar 2011, 4:34 PM
Just wanted to confirm the same bug as I think it is fairly important to get this fixed asap.

I also have another suggestion: wouldn't be interesting to make Store.sync() work similarly with Model.save()? I'm thinking all column values should be updated, not just the id...

For instance suppose we have this model:


Ext.regModel('Application.models.User', {
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' },
...
{ name: 'system_date', type: 'date', dateFormat: 'Y-m-d H:i:s' },
]
});

...where id is a DB-generated identity column and system_date is a timestamp (i.e. 'NOW()' in MySQL). If I populate a User instance and then call save() I'll have both my server-generated field values back on the client.

All this is working perfectly on Ext.data.Model but not on Ext.data.Store.

Also, isn't Store.sync() supposed to take a callback parameter? Right now there's no easy way of knowing when/if/how your store has been synced...

madrabaz
19 Mar 2011, 10:37 AM
Patch:
change
file: src/data/Store.js line: 783
(file: ext-all-debug.js line: 57780)


data.replace(record);

to


if(action === 'create') {
data.replace(data.getKey(me.getNewRecords()[0]), record);
}
else {
data.replace(record);
}

evant
21 Mar 2011, 9:26 PM
Thanks for the report, this should be resolved in the next release.

Dishwasha
18 Sep 2011, 9:56 PM
This problem also currently exists within Sencha Touch 1.1.0. Use the following monkey patch until it is fixed there as well:



(function() {
Ext.override(Ext.data.Store, {
onProxyWrite: function(operation) {
var data = this.data,
action = operation.action,
records = operation.getRecords(),
length = records.length,
callback = operation.callback,
record, i;

if (operation.wasSuccessful()) {
if (action == 'create' || action == 'update') {
for (i = 0; i < length; i++) {
record = records[i];

record.phantom = false;
record.join(this);
if(action == 'create') {
var old = data.findBy(function(item) { return item.phantom == true});
data.replace(old.internalId, record);
} else {
data.replace(record);
}
}
}

else if (action == 'destroy') {
for (i = 0; i < length; i++) {
record = records[i];

record.unjoin(this);
data.remove(record);
}

this.removed = [];
}

this.fireEvent('datachanged');
}


if (typeof callback == 'function') {
callback.call(operation.scope || this, records, operation, operation.wasSuccessful());
}
}
});
})();

naokazu.terada
6 Apr 2012, 1:38 AM
Can anyone post a patch for Sencha Touch 1.1.1 ? ?
I couldnot solve this problem with @Dishwasha (http://www.sencha.com/forum/member.php?93041-Dishwasha)'s patch above.
I tried myself ofcource, but too difficult to me...

OR ...
Is it because I'm trying to catch the new record's id in store's add event listener ?
or, because I make with MVC ??Any relationship ?

Help please ! :((

and, doesn't ST2 have same issue ?