Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ext.JS Prevent Proxy from sending extra 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 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.

like image 273
Arun V Avatar asked Dec 27 '22 11:12

Arun V


2 Answers

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
    }
});
like image 157
nscrob Avatar answered Dec 30 '22 00:12

nscrob


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 },
like image 42
Dariush Jafari Avatar answered Dec 30 '22 00:12

Dariush Jafari