2013-05-02 53 views
0

如何在JSON POST中包含hasOne关联的模型数据?如何使用JSON书写器编写结构化数据?

结构化数据是通过我的网络API需要的形式:

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

回答

0

@nonino

我想我知道该怎么做,但我也有类似的问题。我实际上无法让我的协会给我相关的数据。反正从我凑在互联网上创建一个自定义的作家这样或只是在默认作家getRecordData:功能(记录操作)

,这里是我的自定义作家

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合并为一个json文件。至少这是我希望发生的事情。直到我正确设置了关联后才能测试。

希望这有助于 丹

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