For now I use this code to save new/exist record:
var values = this.getFrmDetails().getValues();
var record = this.getFrmDetails().getRecord();
var store = Ext.getStore('usersStore');
record.set(values);
// Is this a new user?
if (record.data.id==-1)
{
record.save();
store.add(record);
}
store.load();
this.getMainView().pop();
This code works fine, but it always saves & loads the whole data again, I'm tracking records by id
so when I add new record I pass: -1
for id.
If not using store.load()
, the id will be always -1
for new records till I reload my app.
How can I update the new record with id which created at server side?
I use REST proxy with PHP server side, MySQL database.
By the way in Firebug I always see PUT
for new/updated records. How can I make Sencha touch send POST
only for new records and PUT
for just updating existing one?
I have autosync=true
on store.
Thank you
I'm using C# instead PHP but I use the same '-1' Id technique. When I insert a record in the store and sync with remote I return the inserted records and the store takes the values from the service response.
var store = Ext.getStore('fooStore');
var newFoo = {
a: 'a',
b: 'b'
};
store.add(newFoo) // here the record id would be -1
store.sync({
callback: function () {
// here the id is the returned from the server
// this is not needed if you have autoSync, It's just for clearness
}
});
And me C# code structure would be:
function Foo Insert(Foo foo) {
var myTable = new MyTable(); // In PHP would be different, but I guess that the idea is clear.
var newRecord = myTable.Insert(foo); // The insert method returns the added record.
return newRecord;
}
Then the returned record would be taken by the model or the store definition and the Id will be automatically updated. If you use this, there is no need to use the store.load() method, because the response has the needed information for update.
For the kind of request POST instead of PUT, you have to use this in your Model/Store proxy definition:
// ...
proxy: {
// ...
actionMethods: {
create: 'POST',
read: 'POST',
update: 'POST',
destroy: 'POST'
}
// ...
}
// ...