Page 1 of 2 12 LastLast
Results 1 to 10 of 17

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

  1. #1
    Sencha Premium User
    Join Date
    Nov 2016
    Posts
    17

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

    I have a model with a hasMany association. When I first load it from the server it correctly consumes the nested association data and fills out the store. However, when I make some changes to the model and save it the model does not update it's association data that came from the server.

    I have come up with a work around by overriding the save function on my model and parsing out the response from the operation. This doesn't seem ideal though. Does anyone have a suggestion on how to better achieve this?

  2. #2

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Quote Originally Posted by jbeers55 View Post
    I have come up with a work around by overriding the save function on my model and parsing out the response from the operation. This doesn't seem ideal though. Does anyone have a suggestion on how to better achieve this?
    Having the same issue, can you please post your solution? Thanks!

  3. #3
    Sencha User
    Join Date
    Feb 2012
    Location
    annecy france / geneve suisse
    Posts
    149
    Answers
    12

    Default

    Hy,
    here is my solution founded with this thread http://docs.sencha.com/extjs/6.2.0/m...method-getData

    Code:
    Ext.define('ContactBook', {
        extend: 'Ext.data.Model',
        
        uses: [
            'ContactBookContact',
        ],
    
    
        fields: [
            {
                name: 'id',
                type: 'int'
            },{
                ...
            }
        ],
    
    
        hasMany: [
            {
                model: 'ContactBookContact',
                name: 'contacts'
            }
        ],
        
        proxy: {
            type: 'ajax',
            api: {
                ...
            },
            reader: {
                type: 'json',
                rootProperty: 'contacts_books',
                record: 'contact_book'
            },
            writer: {
                type: 'json',
                writeAllFields: true,
                allDataOptions: {
                    associated: true,
                    changes: true,
                    critical: true
                }
            }
        }
    });
    
    Ext.define('ContactBookContact', {
        extend: 'Ext.data.Model',
    
    
        fields: [
            {
                name: 'id',
                type: 'int'
            },{
                name: 'contact_book_id',
                type: 'int',
                critical: 'true'
            },{
                ...
        ],
    });

  4. #4

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Hi Jad, I don't really understand what actually the "solution" is. Can you detail it a bit?
    What is triggering the data associations loading on save() in the upper example?

  5. #5
    Sencha User
    Join Date
    Feb 2012
    Location
    annecy france / geneve suisse
    Posts
    149
    Answers
    12

    Default

    You have to add writer options

  6. #6

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Quote Originally Posted by Jad View Post
    You have to add writer options
    Not sure if we're talking about the same issue.
    The problem: on myModel.save() the server sends back data with associations; after the XHR request the data associations are not loaded automatically like myModel.load() would do it.

  7. #7
    Sencha User
    Join Date
    Feb 2012
    Location
    annecy france / geneve suisse
    Posts
    149
    Answers
    12

    Default

    The save() response is the same of the load() response ? I mean the json is the same ?

  8. #8

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Quote Originally Posted by Jad View Post
    The save() response is the same of the load() response ? I mean the json is the same ?
    Yes, it is.

    In the load call, extjs is doing some trick to load the associations; tried to inject something similar into the save() method, but didn't really got it working.

    http://docs.sencha.com/extjs/6.5.2/c...el-method-load
    Code:
            // Always set the recordCreator. If we have a session, we're already 
            // part of said session, so we don't need to handle that. 
            options.recordCreator = function(data, type, readOptions) {
                // Important to change this here, because we might be loading associations, 
                // so we do not want this to propagate down. If we have a session, use that 
                // so that we end up getting the same record. Otherwise, just remove it. 
                var session = me.session;
                if (readOptions) {
                    readOptions.recordCreator = session ? session.recordCreator : null;
                }
                me.set(data, me._commitOptions);
                //<debug> 
                // Do the id check after set since converters may have run 
                if (doIdCheck && me.getId() !== id) {
                    Ext.raise('Invalid record id returned for ' + id + '@' + me.entityName);
                }
                //</debug> 
                return me;
            };

  9. #9

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Quote Originally Posted by Jad View Post
    The save() response is the same of the load() response ? I mean the json is the same ?
    Yes, it is.

    In the load call, extjs is doing some trick to load the associations; tried to inject something similar into the save() method, but didn't really got it working.

    http://docs.sencha.com/extjs/6.5.2/c...el-method-load
    Code:
            // Always set the recordCreator. If we have a session, we're already 
            // part of said session, so we don't need to handle that. 
            options.recordCreator = function(data, type, readOptions) {
                // Important to change this here, because we might be loading associations, 
                // so we do not want this to propagate down. If we have a session, use that 
                // so that we end up getting the same record. Otherwise, just remove it. 
                var session = me.session;
                if (readOptions) {
                    readOptions.recordCreator = session ? session.recordCreator : null;
                }
                me.set(data, me._commitOptions);
                //<debug> 
                // Do the id check after set since converters may have run 
                if (doIdCheck && me.getId() !== id) {
                    Ext.raise('Invalid record id returned for ' + id + '@' + me.entityName);
                }
                //</debug> 
                return me;
            };

  10. #10

    Join Date
    Sep 2008
    Posts
    142
    Answers
    4

    Default

    Quote Originally Posted by Jad View Post
    The save() response is the same of the load() response ? I mean the json is the same ?
    Yes, it is the same.

Page 1 of 2 12 LastLast

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. Ext JS way to update a record with associations
    By dicorato in forum Sencha Ext JS Q&A
    Replies: 5
    Last Post: 17 Sep 2015, 1:58 PM
  3. 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
  4. Replies: 2
    Last Post: 28 Jul 2012, 11:21 AM
  5. How to load associations after successful Model.save() ?
    By DCstewieG in forum Sencha Ext JS Q&A
    Replies: 2
    Last Post: 9 Jul 2012, 6:01 PM

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
  •