1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    48
    Answers
    3
    Vote Rating
    0
    celwell is on a distinguished road

      0  

    Default Answered: store loading issue with associated models

    Answered: store loading issue with associated models


    Hi,

    I have a model called Event that hasMany Comment records. I have a store of Events. When I load the Events store everything is great. But if I make a server-side change and then call Ext.getStore('Events').load() it only reloads the 'first level' of the Event model. I need it to reload the data of the associated models as well. The proxy is only on the Events store, by the way.

    In event details page/container:
    Code:
        this.getAt(0).getAt(0).setStore(record.photos());
        this.getAt(0).getAt(1).setStore(record.videos());
        this.getAt(6).getAt(1).setStore(record.comments());
    Events model:
    Code:
    Ext.define('WSI.model.Event', {
      extend: 'Ext.data.Model',
      requires: ['WSI.model.Photo', 'WSI.model.Video', 'WSI.model.Comment'],
      config: {
        fields: [
          {
            name: 'id',
            type: 'string'
          }, {
            name: 'title',
            type: 'string'
          }, {
            name: 'description',
            type: 'string'
          }, {
            name: 'category',
            type: 'string'
          }, {
            name: 'dateTimeStart',
            type: 'string'
          }, {
            name: 'dateTimeEnd',
            type: 'string'
          }, {
            name: 'locationName',
            type: 'string'
          }, {
            name: 'locationVicinity',
            type: 'string'
          }, {
            name: 'locationLat',
            type: 'string'
          }, {
            name: 'locationLng',
            type: 'string'
          }, {
            name: 'viewCount',
            type: 'number'
          }, {
            name: 'bookmarked_by_me',
            type: 'boolean',
            defaultValue: false
          }, {
            name: 'flagged_by_me',
            type: 'boolean',
            defaultValue: false
          }
        ],
        hasMany: [
          {
            associatedModel: 'WSI.model.Photo'
          }, {
            associatedModel: 'WSI.model.Video'
          }, {
            associatedModel: 'WSI.model.Comment'
          }
        ]
      }
    });
    Comment Model:
    Code:
    Ext.define('WSI.model.Comment', {
      extend: 'Ext.data.Model',
      config: {
        fields: [
          {
            name: 'id',
            type: 'string'
          }, {
            name: 'event_id',
            type: 'string'
          }, {
            name: 'text',
            type: 'string'
          }, {
            name: 'author',
            type: 'string'
          }, {
            name: 'viewCount',
            type: 'int'
          }, {
            name: 'flagCount',
            type: 'int'
          }, {
            name: 'mobileOrigin',
            type: 'int'
          }, {
            name: 'timestampCreated',
            type: 'string'
          }
        ]
      }
    });
    Events Store:
    Code:
    Ext.define('WSI.store.Events', {
      extend: 'Ext.data.Store',
      xtype: 'eventsstore',
      requires: ['WSI.model.Event'],
      config: {
        model: 'WSI.model.Event',
        pageSize: 15,
        autoLoad: false,
        proxy: {
          type: 'ajax',
          url: 'REMOVED',
          extraParams: {
            sort: 'today',
            category: 'all'
          },
          reader: {
            type: 'json',
            rootProperty: 'events'
          }
        }
      }
    });

    "Refresh All" function in main controller:
    Code:
    refreshAll: function() {
        return Ext.getStore('Events').load({
          scope: this,
          callback: function(records, operation, success) {
            if (success) {
              if (this.getEventDetailsContainer() != null) {
                return this.onViewEventCommand(null, this.getEventDetailsContainer().getEventRecord());
              }
            }
          }
        });
      }

  2. I don't know. Thanks for your help, but I just found out I can do this instead of associations. For the dataview of comments I just config this:


    PHP Code:
    xtype'eventcomments'         
    data: if record.get('comments').length 0 then record.get('comments') else null
    model
    'WSI.model.Comment' 
    and then it doesn't have the refreshing problem. I might come back to this when I have more time, so thanks a lot for your help, but this other way should work for now.

  3. #2
    Sencha User
    Join Date
    Apr 2012
    Posts
    48
    Answers
    3
    Vote Rating
    0
    celwell is on a distinguished road

      0  

    Default


    Please let me know if you need me to provide more information. This problem has been a big time-drainer; so any ideas are welcome!

  4. #3
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64
    Vote Rating
    20
    haduki will become famous soon enough haduki will become famous soon enough

      0  

    Default


    Code:
    hasMany: [
          {
            associatedModel: 'WSI.model.Photo'
          },
         {
            associatedModel: 'WSI.model.Video'
          },
         {
            associatedModel: 'WSI.model.Comment',autoLoad:true
          }    
    ]
    I write English by translator.

  5. #4
    Sencha User
    Join Date
    Apr 2012
    Posts
    48
    Answers
    3
    Vote Rating
    0
    celwell is on a distinguished road

      0  

    Default


    Thanks for the answer. However, it didn't work. I looked at the docs (http://docs.sencha.com/touch/2-0/#!/...y-cfg-autoLoad) and it looks like the autoLoad is only for when it needs to do another AJAX request to get the data. But, my situation is different because the Events store is grabs a response like this:

    PHP Code:
    {
       
    "events":[
          {
             
    "eid":"5079b6d3d6790",
             
    "title":"fdas",
             
    "description":"",
             
    "dateTimeStart":"2012-10-13 11:45:32",
             
    "dateTimeEnd":"2012-10-13 14:45:32",
             
    "category":"",
             
    "viewCount":"7",
             
    "locationName":"The Cellar Restaurant Bar & Grill",
             
    "locationVicinity":"1880 Century Park East, Los Angeles",
             
    "locationLat":"34.061645",
             
    "locationLng":"-118.414493",
             
    "timestampCreated":"2012-10-13 11:45:39",
             
    "photos":[

             ],
             
    "videos":[

             ],
             
    "comments":[
                {
                   
    "cid":"5079b91e3aad63.27417088",
                   
    "aeid":"5079b6d3d6790",
                   
    "text":"lkm",
                   
    "author":"celwell",
                   
    "locationLat":"34.0626475",
                   
    "locationLng":"34.0626475",
                   
    "worthinessCount":"0",
                   
    "viewCount":"0",
                   
    "flagCount":"0",
                   
    "mobileOrigin":"1",
                   
    "timestampCreated":"2012-10-13 11:55:26",
                   
    "deemed_worthy_by_me":false,
                   
    "flagged_by_me":false,
                   
    "id":"5079b91e3aad63.27417088",
                   
    "event_id":"5079b6d3d6790"
                
    },
                {
                   
    "cid":"5079b8f641b1b0.18686446",
                   
    "aeid":"5079b6d3d6790",
                   
    "text":"ewrwer",
                   
    "author":"celwell",
                   
    "locationLat":"34.0626475",
                   
    "locationLng":"34.0626475",
                   
    "worthinessCount":"0",
                   
    "viewCount":"0",
                   
    "flagCount":"0",
                   
    "mobileOrigin":"1",
                   
    "timestampCreated":"2012-10-13 11:54:46",
                   
    "deemed_worthy_by_me":false,
                   
    "flagged_by_me":false,
                   
    "id":"5079b8f641b1b0.18686446",
                   
    "event_id":"5079b6d3d6790"
                
    },
                {
                   
    "cid":"5079b6f2bfe396.71226274",
                   
    "aeid":"5079b6d3d6790",
                   
    "text":"fffff",
                   
    "author":"celwell",
                   
    "locationLat":"34.0626475",
                   
    "locationLng":"34.0626475",
                   
    "worthinessCount":"0",
                   
    "viewCount":"0",
                   
    "flagCount":"0",
                   
    "mobileOrigin":"1",
                   
    "timestampCreated":"2012-10-13 11:46:10",
                   
    "deemed_worthy_by_me":false,
                   
    "flagged_by_me":false,
                   
    "id":"5079b6f2bfe396.71226274",
                   
    "event_id":"5079b6d3d6790"
                
    },
                {
                   
    "cid":"5079b6df6b01d8.90520418",
                   
    "aeid":"5079b6d3d6790",
                   
    "text":"asdf",
                   
    "author":"celwell",
                   
    "locationLat":"34.0626475",
                   
    "locationLng":"34.0626475",
                   
    "worthinessCount":"0",
                   
    "viewCount":"0",
                   
    "flagCount":"0",
                   
    "mobileOrigin":"1",
                   
    "timestampCreated":"2012-10-13 11:45:51",
                   
    "deemed_worthy_by_me":false,
                   
    "flagged_by_me":false,
                   
    "id":"5079b6df6b01d8.90520418",
                   
    "event_id":"5079b6d3d6790"
                
    }
             ],
             
    "bookmarked_by_me":false,
             
    "flagged_by_me":false,
             
    "id":"5079b6d3d6790"
          
    }
       ],
       
    "success":true,
       
    "total":0


    Then the Event model uses the 'comments' data from the response to populate the associated model called Comment. That is, the Comment model doesn't have its own special proxy.

    When I refresh the Events store, the data that my server sends back reflects the latest changes, but for some reason only the Event model gets updated; not the Comment model/store within Event (and also not the Photo and Video models). Which is weird because the first load after app launch always works fine, but all the subsequent loads/refreshes only refresh the Event model.

    Do you understand the problem?

  6. #5
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64
    Vote Rating
    20
    haduki will become famous soon enough haduki will become famous soon enough

      0  

    Default


    Code:
    hasMany: [
        {
            associatedModel: 'WSI.model.Comment',
            reader:{
    	    type:'json',
                rootProperty:'comments'
    	}
        }
    ]
    I write English by translator.

  7. #6
    Sencha User
    Join Date
    Apr 2012
    Posts
    48
    Answers
    3
    Vote Rating
    0
    celwell is on a distinguished road

      0  

    Default


    Hmm... that didn't change anything unfortunately...

  8. #7
    Sencha User
    Join Date
    Apr 2010
    Location
    China
    Posts
    227
    Answers
    64
    Vote Rating
    20
    haduki will become famous soon enough haduki will become famous soon enough

      1  

    Default


    I have tested it,nothing wrong.Maybe i do not understand your question?
    Code:
    Ext.getStore('Events').load(function(records){
        var text=records.getAt(0).comments().getAt(0).get('text');
        console.info(text);
    })
    //console output: lkm
    //change my json data set 'lkm' to 'aaaaaa'
    
    Ext.getStore('Events').load(function(records){
        var text=records.getAt(0).comments().getAt(0).get('text');
        console.info(text);
    });
    //console output: aaaaaa
    I write English by translator.

  9. #8
    Sencha User
    Join Date
    Apr 2012
    Posts
    48
    Answers
    3
    Vote Rating
    0
    celwell is on a distinguished road

      0  

    Default


    I don't know. Thanks for your help, but I just found out I can do this instead of associations. For the dataview of comments I just config this:


    PHP Code:
    xtype'eventcomments'         
    data: if record.get('comments').length 0 then record.get('comments') else null
    model
    'WSI.model.Comment' 
    and then it doesn't have the refreshing problem. I might come back to this when I have more time, so thanks a lot for your help, but this other way should work for now.

Thread Participants: 1

Tags for this Thread