PDA

View Full Version : Store add() + sync() is not generating a server request



ventryj
5 Aug 2013, 5:51 AM
I am trying to add a model instance to a store and sync the store with the server, but there is no request being generated. By no request being generated, I mean that when I monitor the Network tab in my browser console while running the app, there is no POST request being sent to the proxy URL. Other actions, however, such as update and read, are working correctly. Please see the relevant code below. I am grabbing values from a form, adding them to my store, and attempting to sync.

The store:


Ext.define('MyApp.store.Clients', {

extend: 'Ext.data.Store',

requires: [
'MyApp.model.Client'
],


constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
model: 'MyApp.model.Client',
storeId: 'Clients',
pageSize: 15,
proxy: {
type: 'rest',
url: 'api/Client',
reader: {
type: 'json',
root: 'GridData'
}
}
}, cfg)]);
}
});

The model:


Ext.define('MyApp.model.Client', { extend: 'Ext.data.Model',


idProperty: 'ClientNumber',


fields: [
{
name: 'ClientNumber',
type: 'string'
},
{
name: 'Name',
type: 'string'
},
{
name: 'Description',
type: 'string'
},
{
name: 'ValidationKey',
type: 'string'
},
{
name: 'StartDataYearMonth',
type: 'int'
},
{
name: 'NotificationEmail',
type: 'string'
},
{
name: 'ActiveBit',
type: 'boolean'
}
]
});

Controller code:


var form = button.up('form'),
clients = this.getClientsStore(),
vals;


if(form.isValid()) {
vals = form.getValues();
clients.add(vals);
//console.log(clients.getNewRecords());
clients.sync();
form.up('window').close();
} else Ext.Msg.alert('Invalid Data', 'One or more fields as invalid values.');

The form fields:


items: [ {
xtype: 'textfield',
fieldLabel: 'Client Number',
name: 'ClientNumber',
allowBlank: false
},
{
xtype: 'textfield',
fieldLabel: 'Client Name',
name: 'Name',
allowBlank: false
},
{
xtype: 'textfield',
fieldLabel: 'Description',
name: 'Description',
allowBlank: false
},
{
xtype: 'combobox',
fieldLabel: 'Content Start Month',
name: 'StartDataYearMonth',
allowBlank: false,
editable: false,
displayField: 'DataYearMonth',
queryMode: 'local',
store: 'DataYearMonths',
valueField: 'DataYearMonth'
},
{
xtype: 'textfield',
fieldLabel: 'Notification Email',
name: 'NotificationEmail',
vtype: 'email'
},
{
xtype: 'checkboxfield',
fieldLabel: 'Active',
name: 'ActiveBit',
inputValue: '1'
}
]
}

I'm not quite sure what I'm doing wrong, as the controller code appears correct according to the docs and tutorials I've seen. If I un-comment the line in the controller, an empty array is printed, meaning I suppose the store is not registering that a record has been added? No errors are being generated, however. Any help would be greatly appreciated!

aaugen
5 Aug 2013, 11:39 PM
Hi,

I've encountered the same problem in one of my previous Ext JS project.
Try to declare your rest proxy on model's instead on your store.
It seems that the rest proxies only work on models.

I detected an other problem with form.getValues() because this method doesn't return an model but a set of key/value pairs, so you can't add this on your store.
Depending on your version of Ext JS, one solution is to use form.loadRecord with new empty record to associated model to the form, execute form.updateRecord to valuate this record, get your record with form.getRecord and finally add record to your store and execute sync method.

ventryj
6 Aug 2013, 5:23 AM
Thanks for the response. I'm not sure if it's accurate to say that rest proxies do not work for stores, as read, update, and delete actions all seem to work appropriately. I believe it is just the create actions that aren't working properly. As to your second point, I thought of this as well, but I saw no difference in results when I created a new model instance like the following:


var client = Ext.create("MyApp.model.Client", vals);

And then subsequently added client to my store. For now, I have switched to using form submits for create actions, but I am still curious as to whether this is a bug since other users have experienced this same problem.

Farish
6 Aug 2013, 5:41 AM
when you add records to your store, their id should be 0 and not a non-zero value otherwise the store thinks that they are not new records. also check the phantom flag of the newly added record. this must be set to true (automatically).

fschaeffer
6 Aug 2013, 9:40 PM
As Farish already said, the id field has to be 0 otherwise there will be no POST request. Your form has ClientNumber as blank:false so I assume that this value will be always something different than 0. And as you define clientNumber as idProperty you will never get any POST request....