Reputation: 590
Here is my model:
Ext.define('A.model.Group', {
extend: 'Ext.data.Model',
fields:['id', 'name'],
proxy: {
type: 'rest',
url: '/group',
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json',
writeAllFields: false
}
}
});
The model is being used in a Tree via a TreeStore
The problem is that when a PUT
, POST
or DELETE
method is done, instead of sending only fields from the model in the JSON payload, fields from Ext.data.NodeInterface
are also sent. Here is an example payload:
{"id":"","name":"TestGroup","parentId":"8","index":0,"depth":3,"checked":null}
I don't want the extra fields parentId
, index
, depth
, and checked
to be sent. What is the best way to do this? I don't want to have to ignore them on the server.
Upvotes: 4
Views: 4469
Reputation: 5443
the NodeInterface
adds these fields into your model:
{name: 'parentId', type: idType, defaultValue: null},
{name: 'index', type: 'int', defaultValue: null, persist: false},
{name: 'depth', type: 'int', defaultValue: 0, persist: false},
{name: 'expanded', type: 'bool', defaultValue: false, persist: false},
{name: 'expandable', type: 'bool', defaultValue: true, persist: false},
{name: 'checked', type: 'auto', defaultValue: null, persist: false},
{name: 'leaf', type: 'bool', defaultValue: false},
{name: 'cls', type: 'string', defaultValue: null, persist: false},
{name: 'iconCls', type: 'string', defaultValue: null, persist: false},
{name: 'icon', type: 'string', defaultValue: null, persist: false},
{name: 'root', type: 'boolean', defaultValue: false, persist: false},
{name: 'isLast', type: 'boolean', defaultValue: false, persist: false},
{name: 'isFirst', type: 'boolean', defaultValue: false, persist: false},
{name: 'allowDrop', type: 'boolean', defaultValue: true, persist: false},
{name: 'allowDrag', type: 'boolean', defaultValue: true, persist: false},
{name: 'loaded', type: 'boolean', defaultValue: false, persist: false},
{name: 'loading', type: 'boolean', defaultValue: false, persist: false},
{name: 'href', type: 'string', defaultValue: null, persist: false},
{name: 'hrefTarget', type: 'string', defaultValue: null, persist: false},
{name: 'qtip', type: 'string', defaultValue: null, persist: false},
{name: 'qtitle', type: 'string', defaultValue: null, persist: false},
{name: 'children', type: 'auto', defaultValue: null, persist: false}
two of them dont have `persist' property.
Most of NodeInterface's fields default to persist: false. This means they are non-persistent fields by default. Non-persistent fields will not be saved via the Proxy when calling the TreeStore's sync method or calling save() on the Model. In most cases, the majority of these fields can be left at their default persistence setting, but there are cases where it is necessary to override the persistence of some fields. The following example demonstrates how to override the persistence of a NodeInterface field. When overriding a NodeInterface field it is important to only change the persist property. name, type, and defaultValue should never be changed.
Override the fields like this:
{ name: 'iconCls', type: 'string', defaultValue: null, persist: true },
Upvotes: 3
Reputation: 11137
You can add a serializer to the fields in the model you do not want to send to the server.
var makeUndefined = function(value, record) {
return undefined;
}
var fieldsOfYourModel = [
{
serialize: makeUndefined,
name: 'parentId'
},
{
serialize: makeUndefined,
name: 'index'
}
];
serialize
is a function which converts the Model's value for this Field into a form which can be used by whatever Writer is being used to sync data with the server. http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Field-cfg-serialize
serialize
is available since Ext JS 4.1.1.
Upvotes: 2
Reputation: 4493
If you wan't to send only specific data to server, writeAllFields is not the solution as if is set to false it only sends the modified fields.
The solution for your problem is defining your own writer and overriding the method getRecordData
here is a posible example:
var newWriter = Ext.create('Ext.data.writer.Json',{
getRecordData: function(record){
return {'id':record.data.id,'name':record.data.name};
}
})
Ext.define('A.model.Group', {
extend: 'Ext.data.Model',
fields:['id', 'name'],
proxy: {
type: 'rest',
url: '/group',
reader: {
type: 'json',
root: 'data'
},
writer: newWriter
}
});
Upvotes: 5