Results 1 to 3 of 3

Thread: Load nested data on record save

  1. #1
    Sencha User
    Join Date
    Apr 2007
    Posts
    39
    Answers
    2

    Default Load nested data on record save

    I want to create a 'details' record on the server and send it back when the 'master' is created.

    The 'details' record is NOT created in the front. Instead the 'master' record is created and posted.
    When the response is received the 'details' record is also part of the response.

    I have set up the associations between the 'master' and the 'details'. However, the 'master' does not read the 'details' array in the response.


    Master model -
    PHP Code:
    Ext.define('App.model.Master', {
       
    extend    'Ext.data.Model'

      
    ,fields    : [
        { 
    name  'master_name'type  'string' },
        { 
    name  'id'type  'int'}
      ]
      ,
    proxy    : {
        
    type  'rest',
        
    url    '/master',
        
    reader    : {
          
    type  'json',
          
    root  'masters'
        
    }
      }
      ,
    associations  : [{
        
    type      'hasMany'
        
    id        'id',
        
    foreignKey    'master_id',
        
    associationKey  'details',
        
    name      'details',
        
    model      'App.model.Detail',
      }]
    }); 
    Master Store -
    PHP Code:
    Ext.define('App.store.Master', {

         
    extend        'ScaleApp.store.Store'
        
    ,model        'App.model.Master'
    }); 
    Detail model -
    PHP Code:
    Ext.define('App.model.Detail', {
      
    extend    'Ext.data.Model'
      
    fields    : [
        { 
    name  'detail_name'type  'string' },
        { 
    name  'id'type  'int' },
        {
    name  'master_id'type  'string'}
      ]
    }); 
    I'm adding the record like this -
    PHP Code:
    var master_record = new App.model.Master({
        
    master_name    'aaa'
    });
    var 
    store Ext.StoreMgr.get('Master');
    store.add(master_record);
    store.sync(); 
    The response object I get back is -
    PHP Code:
    {
      
    success true,
      
    masters : {
        
    master_name  'aaa',
        
    id      1,
        
    details    : [
          {
            
    detail_name  'bbb',
            
    id      11,
            
    master_id  1
          
    }
        ]
      }

    I was expecting that the 'record.details()' would give me the one record that came back on adding of the Master, but I get nothing.

    However, if I reload the store, I'm able to get the data correctly.

    I need to be able to read the nested response on Store.add() as well.

    Any suggestions?

    Thanks in advance.

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,379
    Answers
    3997

    Default

    Associated data needs to be run through the reader in order to work. In these cases they are not run through the reader properly or at all for associations to work.
    Mitchell Simoens @LikelyMitch

    Check out my GitHub:
    https://github.com/mitchellsimoens

    Posts are my own, not any current, past or future employer's.

  3. #3
    Sencha User
    Join Date
    Apr 2007
    Posts
    39
    Answers
    2

    Default

    Thanks Mitchell, I got that. Is there any workaround that you can suggest?

    Currently, I'm doing this -

    PHP Code:
    Ext.StoreMgr.get('Master').on({
      
    write: function(stoperation){
        if (
    operation.action == 'create'){
          var 
    resp_obj Ext.decode(operation.response.responseText);
          
          if (
    resp_obj.masters.details){
            var 
    rec operation.records[0];
            var 
    details_store  rec.details();
            var 
    details_records  resp_obj.masters.details;
            
            
    details_store.add(details_records);
          }
        }
      }
    }); 
    Is there a better way?

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
  •