Problems met when using extjs association model with mongoose nested shcema, sub data missing in post Problems met when using extjs association model with mongoose nested shcema, sub data missing in post mongoose mongoose

Problems met when using extjs association model with mongoose nested shcema, sub data missing in post


Solved this problem finally, the key is that Ext.data.writer.Json in ExtJS 4 does not support association well.

http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store

This link offered some solutions.

I use Extjs 4.1.1 a, and add the DeepJson into the Extjs 4.1 source folder:

/*** @class Ext.data.writer.DeepJson This class is used to write *        {@link Ext.data.Model} data to the server in a JSON format. *  * It overrides the original Ext.data.writer.Json since the Json can not handle * hasMany association, it can only transform the outside part into Json, the * inside data will be omiited. *  * @Yi Fang Reference: *     http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store/page3     23 Mar 2012 6:00 AM *     http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store/page5     13 Feb 2013 2:57 PM *      */Ext.define('Ext.data.writer.DeepJson', {        extend : 'Ext.data.writer.Json',        getRecordData : function(record, operation) {            // Setup variables            var me = this, i, association, childStore, data;            data = me.callParent(arguments);            // Iterate over all the hasMany associations            for (i = 0; i < record.associations.length; i++) {                association = record.associations.get(i);                if (association.type == 'hasMany') {                    data[association.name] = null;                    childStore = record[association.storeName];                    // Iterate over all the children in the current                    // association                    childStore.each(function(childRecord) {                                if (!data[association.name]) {                                    data[association.name] = [];                                }                                // Recursively get the record data for                                // children (depth first)                                var childData = this.getRecordData.call(                                        this, childRecord);                                /*                                 * If the child was marked dirty or phantom                                 * it must be added. If there was data                                 * returned that was neither dirty or                                 * phantom, this means that the depth first                                 * recursion has detected that it has a                                 * child which is either dirty or phantom.                                 * For this child to be put into the                                 * prepared data, it's parents must be in                                 * place whether they were modified or not.                                 */                                if (childRecord.dirty                                        || childRecord.phantom                                        || (childData != null)) {                                    data[association.name].push(childData);                                    record.setDirty();                                }                            }, me);                    /*                     * Iterate over all the removed records and add them to                     * the preparedData. Set a flag on them to show that                     * they are to be deleted                     */                    Ext.each(childStore.removed, function(                                    removedChildRecord) {                                // Set a flag here to identify removed                                // records                                removedChildRecord.set('forDeletion', true);                                var removedChildData = this.getRecordData                                        .call(this, removedChildRecord);                                data[association.name]                                        .push(removedChildData);                                record.setDirty();                            }, me);                }            }            // Only return data if it was dirty, new or marked for deletion.            if (record.dirty || record.phantom || record.get('forDeletion')) {                return data;            }            return null;        }    });

In the PvdcPrice model, update the proxy as

proxy : {            type : 'rest',            url : '/pvdcprices',            reader : {                type : 'json',                root : 'data',                successProperty : 'success'            },            writer : Ext.create('Ext.data.writer.DeepJson')        }

Then the post for nested data works.


belongsTo: 'App.model.PvdcPrice' is missing in 'App.model.PvdcPriceDetail' model class. Its an association problem in your code and you need to map the parent class key with child model. please refer below link (in association session).

http://docs.sencha.com/extjs/4.2.0/#!/guide/data

I hope after making your belongsTo config to 'App.model.PvdcPriceDetail' class it will work fine as expected.

Thanks