ExtJS 4.1 - Returning Associated Data in Model.Save() Response

前端 未结 4 1157
广开言路
广开言路 2021-01-04 08:09

I am curious as to why the record contained in the result set of a Model.save() response does not properly return updated associated data, despite the updated d

4条回答
  •  庸人自扰
    2021-01-04 08:52

    In ExtJS 6.2 the problem still (or again exists). My solution:

    /**
     * In Ext.data.reader.Reader::extractRecord the call readAssociated reads out the hasMany associations and processes them.
     * This works perfectly for Model.load() since internally a Model is used as record variable in extractRecord. 
     * For Model.save() record extractRecord contains just the Object with the received data from the PUT request, 
     *  therefore readAssociated is never called and no associations are initialized or updated.
     * The following override calls readAssociated if necessary in the save callback.
     */
    Ext.override(Ext.data.Model, {
        save: function(options) {
            options = Ext.apply({}, options);
            var me = this,
                includes = me.schema.hasAssociations(me),
                scope  = options.scope || me,
                callback,
                readAssoc = function(record) {
                    //basicly this is the same code as in readAssociated to loop through the associations
                    var roles = record.associations,
                        key, role;
                    for (key in roles) {
                        if (roles.hasOwnProperty(key)) {
                            role = roles[key];
                            // The class for the other role may not have loaded yet
                            if (role.cls) {
                                //update the assoc store too                            
                                record[role.getterName]().loadRawData(role.reader.getRoot(record.data));
                                delete record.data[role.role];
                            }
                        }
                    }
    
                };
    
            //if we have includes, then we can read the associations
            if(includes) {
                //if there is already an success handler, we have to call both
                if(options.success) {
                    callback = options.success;
                    options.success = function(rec, operation) {
                        readAssoc(rec);
                        Ext.callback(callback, scope, [rec, operation]);
                    };
                }
                else {
                    options.success = readAssoc;
                }
            }
            this.callParent([options]);
        }
    });
    

提交回复
热议问题