【问题标题】:How to write structured data with JSON writer?如何使用 JSON 编写器编写结构化数据?
【发布时间】:2013-05-02 18:11:10
【问题描述】:

如何在 JSON POST 中包含 hasOne 关联的模型数据?

我的 Web API 需要以下形式的结构化数据:

{
    id: 1234,
    name: 'Aaron Smith',
    address: {
        address1: '1925 Isaac Newton Sq',
        address2: 'Suite 300',
        city: 'Reston',
        state: 'VA',
        zip: 20190
    }
}

【问题讨论】:

    标签: extjs extjs4 extjs4.2


    【解决方案1】:

    @nonino

    我想我知道该怎么做,但我也遇到了类似的问题。我实际上无法让我的关联给我相关的数据。无论如何,根据我在互联网上搜索的内容,制作一个像这样的自定义编写器,或者只是在默认编写器中 getRecordData: function(record,operation)

    这是我的自定义作家

    Ext.define('Wakanda.writer', {
    
        extend: 'Ext.data.writer.Json',
    
       // alternateClassName: 'SimplyFundraising.data.WakandaWriter',
    
        alias: 'writer.wakanda',
    
        writeAllFields: false,
    
        getRecordData: function(record,operation) {
            debugger;
            Ext.apply(record.data,record.getAssociatedData());
            debugger;
            var isPhantom = record.phantom === true,
                writeAll = this.writeAllFields || isPhantom,
                nameProperty = this.nameProperty,
                fields = record.fields,
                data = {},
                changes,
                name,
                field,
                key;
    
            if (writeAll) {
                // console.log("getRecordData1", this, arguments);
                fields.each(function(field){
                    if (field.persist) {
                        debugger;
                        name = field[nameProperty] || field.name;
                        data[name] = record.get(field.name);
                    } else {
    
                    }
    
                });
            } else {
                changes = record.getChanges();
                debugger;
                // console.log("getRecordData2", this, arguments, changes);
                for (key in changes) {
                    if (changes.hasOwnProperty(key)) {
                        field = fields.get(key);
                        name = field[nameProperty] || field.name;
                        data[name] = changes[key];
                    }
                }
                if (!isPhantom) {
                    debugger;
    
                    data[record.idProperty] = record.getId();
                    if(operation.action !== 'destroy'){
                    data[record.stampProperty] = record.get(record.stampProperty);
                    }
                }
            }
            return {'__ENTITIES': [data]};
        }
    
    });
    

    我认为关键是在 getRecordData 中,我有一个语句 Ext.apply(record.data,record.getAssociatedData());如果 record.getAssociatedData 确实返回了您的数据,那么 Ext.apply 语句会将您当前的 record.data 与您的 record.getAssociatedData 合并到 1 个 json 文件中。至少这是我希望发生的事情。在我正确设置关联之前无法测试。

    希望这会有所帮助, 丹

      getRecordData: function(record,operation) {
            debugger;
            Ext.apply(record.data,record.getAssociatedData());
            debugger;
    

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2012-06-28
      • 2012-12-19
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多