【问题标题】:Ext.JS Prevent Proxy from sending extra fieldsExt.JS 防止代理发送额外的字段
【发布时间】:2015-02-01 23:06:11
【问题描述】:

这是我的模型:

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
        }
    }
});

模型通过 TreeStore 在树中使用

问题在于,当 PUTPOSTDELETE 方法完成时,不是只发送 JSON 有效负载中来自模型的字段,而是来自 Ext.data.NodeInterface 的字段。这是一个示例负载:

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

我不希望发送额外的字段 parentIdindexdepthchecked。做这个的最好方式是什么?我不想在服务器上忽略它们。

【问题讨论】:

    标签: json extjs tree extjs4


    【解决方案1】:

    如果您不想仅向服务器发送特定数据,则 writeAllFields 不是解决方案,因为如果设置为 false,它只会发送修改后的字段。 您的问题的解决方案是定义您自己的编写器并覆盖方法getRecordData 这是一个可能的示例:

    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
        }
    });
    

    【讨论】:

      【解决方案2】:

      NodeInterface 将这些字段添加到您的模型中:

      {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}
      

      其中两个没有 `persist' 属性。

      NodeInterface 的大部分字段默认为persist: false。这意味着默认情况下它们是非持久字段。在调用 TreeStore 的同步方法或调用 Model 上的 save() 时,不会通过 Proxy 保存非持久字段。在大多数情况下,这些字段中的大多数可以保留其默认持久性设置,但在某些情况下,需要覆盖某些字段的持久性。以下示例演示如何覆盖 NodeInterface 字段的持久性。覆盖 NodeInterface 字段时,仅更改持久属性很重要。永远不要更改名称、类型和默认值。
      覆盖这样的字段:

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

      【讨论】:

      • 这是最优雅的解决方案,因为它只使用配置,不需要覆盖低级函数。
      【解决方案3】:

      您可以将序列化程序添加到模型中您不想发送到服务器的字段。

      var makeUndefined = function(value, record) {
          return undefined;
      }
      
      var fieldsOfYourModel = [
          {
              serialize: makeUndefined,
              name: 'parentId'
          },
          {
              serialize: makeUndefined,
              name: 'index'
          }
      ];
      

      serialize 是一个函数,它将模型的该字段的值转换为可以被任何 Writer 用于与服务器同步数据的形式。 http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Field-cfg-serialize

      serialize 自 Ext JS 4.1.1 起可用。

      【讨论】:

      • 优秀的解决方案!您暗示了这一点,但没有明确地说出来(这对我来说并不直观):如果 serialize 方法返回 undefined 该字段被完全排除在提交给服务器之外(它不提交 undefined 或类似的)
      猜你喜欢
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多