Ext.JS Prevent proxy server from sending additional fields

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 used in the tree through TreeStore.

The problem is that when the PUT , POST or DELETE method is executed, instead of sending only the fields from the model in the JSON payload, the fields from Ext.data.NodeInterface also sent. Here is an example payload:

 {"id":"","name":"TestGroup","parentId":"8","index":0,"depth":3,"checked":null} 

I do not want additional fields parentId , index , depth and checked sent. What is the best way to do this? I do not want to ignore them on the server.

+4
source share
3 answers

If you do not want to send only certain data to the server, writeAllFields is not a solution, as if it is set to false, it sends only the changed fields. The solution to your problem is to define your own author and override the getRecordData method getRecordData here is an 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 } }); 
+5
source

NodeInterface adds these fields to 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 do not have the `persist 'property.

Most default NodeInterface fields for persist: false. This means that by default they are inconsistent. Continuous fields will not be saved through the proxy when calling the TreeStore synchronization method or calling save () in the model. In most cases, most of these fields can be left by default, but there are cases when you need to override the saving of some fields. The following example shows how to override the persistence of a NodeInterface field. When overriding the NodeInterface field, it is important to change the persist property. name, type and defaultValue should never be changed.
Override the following fields:

  { name: 'iconCls', type: 'string', defaultValue: null, persist: true }, 
+3
source

You can add a serializer to the fields in the model that 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 that converts the Model value for this field into a form that can be used by any Writer to synchronize data with the server. http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Field-cfg-serialize

serialize is available with Ext JS 4.1.1.

+2
source

Source: https://habr.com/ru/post/1379574/


All Articles