0

如何在 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
    }
}
4

1 回答 1

0

@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;
于 2013-05-13T17:51:27.590 回答