1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    16
    Vote Rating
    0
    Corcho is on a distinguished road

      0  

    Default Stores: add() nested Models problem

    Stores: add() nested Models problem


    Hi, it's been more like 8 hours of debugging and researching my problem... I come here as my last resort.

    Im trying to use the add() method of store to add a new record. The trick here is the model has a hasMany property. This is the definition code of the models:

    Code:
    Ext.define('MyApp.model.Notification', {
        extend: 'Ext.data.Model'
         
        ,requires: [
            'MyApp.model.notification.NotificationButton'
        ]
        ,fields: [{
             name: 'type'
            ,type: 'string'
        },{
             name: 'icon'
            ,type: 'string'
        },{
             name: 'description'
            ,type: 'string'
        }]
        
        ,hasMany: {
             model: 'MyApp.model.notification.NotificationButton'
            ,name: 'buttons'
        }
    });
    
    Ext.define('MyApp.model.notification.NotificationButton', {
         extend: 'Ext.data.Model'
        ,fields: [{
             name: 'text'
            ,type: 'string'
        },{
             name: 'image'
            ,type: 'string'
        },{
             name: 'id'
            ,type: 'string'
        },{
             name: 'fn'
            ,type: 'string'
        }]
        
        ,belongsTo: 'MyApp.model.Notification'
    });
    And this is an example of the object:

    Code:
    {    notifications: [{
             "type": "warning"
            ,"icon": "resources/images/myImage.png"
            ,"description": "desc"
            ,"buttons": [{
                 "id": 'btn-1'
                ,"text": "YES"
                ,"image": "resources/images/image2.png"
                ,"fn": "example"
            },{
                 "id": 'btn-2'
                ,"text": "LATER"
                ,"image": "resources/images/image3.png"
                ,"fn": "example"
            }]
        }]
    }
    The weird thing is that if I set the store to autoload a JSON file like above, it works fine and the buttons are added corretly. BUT, when I fire the ADD() method of the store and pass the same JSON object, Notification model is added well, but not with the buttons. It's like the "Notification" model is not calling the "NotificationButton" model. To be clear: when I fire add() method, a "Notification" record is added and "buttons" is added like a simple array and not as a model.

    This is how I'm trying to add the new record:

    Code:
    var test= Ext.create('MyApp.model.Notification', {
             "type": "warning"
            ,"icon": "resources/images/myImage.png"
            ,"description": "desc"
            ,"buttons": [{
                 "id": 'btn-1'
                ,"text": "YES"
                ,"image": "resources/images/image2.png"
                ,"fn": "example"
            },{
                 "id": 'btn-2'
                ,"text": "LATER"
                ,"image": "resources/images/image3.png"
                ,"fn": "example"
            }]
        });
            Ext.getStore(storeId).add(test);
    And this is what the console shows me:
    Code:
    newClass {phantom: true, internalId: "ext-record-26", raw: undefined, data: Object, modified: Object}
    • data: Object
      • buttons: Array[2]
        • 0: Object
          • id: "btn-1"
          • image: "resources/images/image2.png"
          • text: "YES"
          • __proto__: Object
        • 1: Object
        • length: 2
        • __proto__: Array[0]
      • description: "desc"
      • icon: "resources/images/myImage.png"
      • type: "warning"
      • __proto__: Object
    • dirty: false
    • editing: false
    • events: Object
    • id: "MyApp.model.Notification-ext-record-26"
    • internalId: "ext-record-26"
    • modified: Object
    • phantom: true
    • raw: undefined
    • store: newClass
    • __proto__: prototype
    Is there something I'm doing wrong here? I tryed some other approaches and none worked for me. Any help or approach would be appreciated.

    Bye!

    MORE INFO:
    This is the record when I set the store to autoload, and what I spect to get when I add a new one. See how "buttons" are shown as records and not as an array:
    Code:
    • 0: newClass
      • buttonsStore: newClass
      • data: Object
      • dirty: false
      • editing: false
      • events: Object
      • id: "MyApp.model.Notification-ext-record-26"
      • index: 0
      • internalId: "ext-record-26"
      • modified: Object
      • phantom: true
      • raw: Object
      • store: newClass
      • __proto__: prototype
    Finally, came up to this post that sais it's a "bug" in 4.0. Anyone with an idea to fix this in 4.0?
    Last edited by Corcho; 11 Feb 2013 at 10:53 AM. Reason: Hint

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    The data has to get read by the reader which is why when you load the json it works.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

Thread Participants: 1

Tags for this Thread