PDA

View Full Version : make store to fire create event.



Warstone
5 Feb 2013, 4:47 AM
How I can create new record on client and server side?
store.add({<params>}) create record only on client side. Any sync call dosn't do anything. Can someone give me a working demo of it? (doc demos dosn't answer on this question)

sdt6585
5 Feb 2013, 5:25 AM
The key to getting the store's sync method to work is to have a properly configured proxy with any necessary writer configurations needed for your situation. This can be done either on the store or the model but must be in one of the two places. Also, I find it more intuitive to create new records from the model rather than the store, but either will work. Here is an example of it in the model.

Model


Ext.define('App.models.Action', {
extend: 'Ext.data.Model',

proxy: {
type: 'ajax',
api: {
create : App.c.locations.SERVER + 'Action/Create',
read : App.c.locations.SERVER + 'Action/Read',
update : App.c.locations.SERVER + 'Action/Update',
destroy : App.c.locations.SERVER + 'Action/Delete'
},
reader: {
type: 'json',
root: 'data',
successProperty: "success"
},
writer: {
writeAllFields: false
}
},

fields: [
{ name: 'id', type: 'int' , useNull: true },
{ name: 'Name', type: 'string' , useNull: true }
]
});


Store:


Ext.define('App.stores.Action', {
extend: 'Ext.data.Store',

model: App.models.Action
});


Usage:


//Create action and add to store
App.models.Action.create({
Name: 'test'
}).save({
scope: this,
success: function (action) {
//Add it to the resource store
actionStoreInstance.add(action);
}
});

Warstone
5 Feb 2013, 5:30 AM
In fact... I get answer already like this:
var rec = me.store.createModel(form.getFieldValues());
rec.phantom = true;
me.store.insert(0, rec);If there is autoSync, then it will save at add. If not, than next sync will batch it. But guys!!! No one say it at add method??

Why there is no simple "one call" api for create phantom record???

Warstone
5 Feb 2013, 5:33 AM
Next question is how I can set id from server to new record??? There must be default method... What I nedd to return from server? success: true and?

Warstone
5 Feb 2013, 5:37 AM
//Create action and add to store
App.models.Action.create({
Name: 'test'
}).save({
scope: this,
success: function (action) {
//Add it to the resource store
actionStoreInstance.add(action);
}
});
Btw, as i know, this will fire update url, but not create. For create url you nedd to set phantom there after creation,

sdt6585
5 Feb 2013, 5:51 AM
I have never had to set a phantom property to get create to work. I believe it uses the id property from the model (whether it's set or not) to determine whether to create or update the record. If the primary key for your model is something other than 'id', you should let Ext know using the idProperty config on the model.

As to what you should return from the server, it is set up to recieve a full copy of the record back including the id generated on the server side. This is what my server sends back:


{"success":true,"message":"ok","data":[{"id":"11","Name":"test"}]}

Finally, I have found the add method with an object definition and the store.sync() method to work fine. You should be able to do something like:


store.add({Name: 'Test'});
store.sync();

Warstone
5 Feb 2013, 10:19 AM
ExtJS 4.0.7. There is no id property on the form. There is id column in data and in model. Debugging add method showed me, that created record has phantom = false.

And BTW... Nothing about "how it works" in docs ((( I think that ExtJS was developed by Windows users, that only learn how to do this or that. Linux world made on "you must know what you want to do, how it works, etc"

sdt6585
5 Feb 2013, 11:08 AM
The documentation for the idProperty can be found here for 4.0.7:

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Model-cfg-idProperty

It is part of the model configuration not the form and would only apply if your primary key/id property is something other than id. Are you setting the id property when you create the new record? That should be the only reason that phantom is set to false before the record is created server side. You might try setting a break-point starting with your store.add({}) call and follow it through the framework to see what's going on. Post your sent and recieved xhr requests here plus some code examples if you need further assistance.

Warstone
5 Feb 2013, 12:15 PM
You can see code earler in the topic. All fields got directly from form and there is no id there.