0

How can I include hasOne associated model data in the JSON POST?

Structured data is required by my web API in the form of:

{
    id: 1234,
    name: 'Aaron Smith',
    address: {
        address1: '1925 Isaac Newton Sq',
        address2: 'Suite 300',
        city: 'Reston',
        state: 'VA',
        zip: 20190
    }
}
tonymayoral
  • 4,797
  • 2
  • 26
  • 27

1 Answers1

0

@nonino

I think I know how to do it but I am also having a similar problem. I can't actually get my associations to give me the associated data. Anyway from what I have scrounged on the internet make a custom writer like this or just in the default writers getRecordData: function(record,operation)

Here is my custom writer

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

});

The key I think is in the getRecordData where I have a statement Ext.apply(record.data,record.getAssociatedData()); If record.getAssociatedData does indeed return your data then the Ext.apply statement will merge your current record.data with your record.getAssociatedData into 1 json file. At least this is what I hope happens. Can't test until I get my associations setup correctly.

Hope this helps, Dan

  getRecordData: function(record,operation) {
        debugger;
        Ext.apply(record.data,record.getAssociatedData());
        debugger;
dan
  • 2,857
  • 6
  • 34
  • 60