Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Using model.save() to update a record isn't loading associations.

  1. #11
    Sencha User Jad's Avatar
    Join Date
    Feb 2012
    Location
    annecy france / geneve suisse
    Posts
    149
    Answers
    12

    Default

    and the json on the save response has the right values ?

  2. #12

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Quote Originally Posted by Jad View Post
    and the json on the save response has the right values ?
    Yes, it has. I will try to provide an example. On save the associations are not loaded. It is not implemented, had a look in the code of extjs.
    Management means doing the things right,
    Leadership means doing the right things.
    www.interpid.eu

  3. #13
    Sencha Premium User
    Join Date
    Nov 2016
    Posts
    17

    Default

    Quote Originally Posted by klodoma1 View Post
    Yes, it has. I will try to provide an example. On save the associations are not loaded. It is not implemented, had a look in the code of extjs.
    OP here, the team that I work with spent a lot of time working on this and unfortunately we have come to the conclusion that you cannot do this in extjs. Our "fix" initially was to loop over the response data, grab the association store, remove its records and then add back the data returned by the server. This turned out to cause issues though as the actually model instance would be different than the original one and it would cause problems in our application logic.

    Extjs gets around this issue by making individual POST/PUT requests to the server so that in the response you know exactly which model instance to update.

    In the end we ended up solving the issue by saving the record and then completely reloading the record with a record.load( {id} ) call. This is certainly less then ideal but its a minor time cost for the least bug prone implementation.

  4. #14
    Sencha User
    Join Date
    Apr 2013
    Posts
    908
    Answers
    105

    Default

    I guess you have seen this fiddle and it didn't help?

  5. #15
    Sencha Premium User
    Join Date
    Nov 2016
    Posts
    17

    Default

    Quote Originally Posted by hakimio View Post
    I guess you have seen this fiddle and it didn't help?
    I had not seen that fiddle. However, it doesn't properly update the association data when you save. I've forked the fiddle and added some code to help explain the problem.

    If you look at the user-update.json file you'll see that the update returns age: 99 and a "corrected" address to simulate data being processed on the server side. Similar to address validation or something. When you save you'll notice that the persons age will correctly update to 99 from whatever you set it to but that the address will not. The address will never reflect what was "sent back" in the save response.

    This demonstrates that association data does not get read back on a save only on a load().

    The way we've chose to work around this in our project is to get the id of the record after saving it and then call a complete new load(). Obviously that is an expensive operation so we limit it to areas where we really need to load nested data.

  6. #16
    Sencha User
    Join Date
    Oct 2016
    Posts
    28
    Answers
    1

    Default

    I had the same issue (I asked a related question here: https://stackoverflow.com/questions/...ear-dirty-flag). Also another related topic is here: https://www.sencha.com/forum/showthread.php?430854

    As I understood, the framework does not set the values received from server. Instead, it is setting the value that you sent to server after a succesfull response was received.

    @jbeers55 In my case, the workaround was similar to yours: I loaded the entire store. This solution is not very efficient when you have large amount of data.

    Does record.load( {id} ) is the same as store.load( {id } )?

  7. #17
    Sencha User
    Join Date
    May 2009
    Location
    Germany
    Posts
    54

    Default

    Hi there, I had the same problem. My workaround:
    Code:
    /**
     * 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]);
        }
    });

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Model.save allDataOptions.serialize with associations
    By incutonez in forum Ext JS 6.x Q&A
    Replies: 2
    Last Post: 11 Mar 2019, 4:50 AM
  2. atomic save record along with hasMany associations
    By vmihaylov in forum Ext 5: Q&A
    Replies: 1
    Last Post: 27 Mar 2015, 7:04 AM
  3. Replies: 2
    Last Post: 28 Jul 2012, 11:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •