backbone.js - Collection renders fine when loaded from remote Url, but not when loaded from localStorage (initialize vs. onRender) -


i'm using layout's 'initialize' method instantiate collection, fetch() data url on collection, , instantiate new collectionview passing through collection it's fetched data. collectionview later gets 'shown' in region.

it works perfectly.

i wanted switch on using localstorage retrieve data, instead of remote url. saved data retrieved remote url localstorage. updated collection 'localstorage: new backbone.localstorage('entries')'. retrieves data localstorage. but. none of regions in layout rendered (i've tried calling this.render() in various places no luck).

if replace 'initialize' method 'onrender' in layout responsible instantiating everything, renders (regardless of whether came remote url or localstorage).

why if use layout's 'initialize' method fetch data in collection localstorage, nothing gets rendered; whereas rendered if data fetched remote url? why both work when using 'onrender' method?

some code reference:

    var entriescollection = backbone.collection.extend({         model: entrymodel,         url: '/api/entries',         localstorage: new backbone.localstorage('entries')     });      return entrieslayout = marionette.layout.extend({      ...      // below, if use onrender works, if change 'initialize'     // works when data loaded via remote url, not localstorage. (the collection     // populated data in both cases.)      onrender: function() {          ...          this.entries_collection = new entriescollection();         var self = this;         this.entries_collection.fetch().done(function() {            /*             self.entries_collection.localstorage = new backbone.localstorage('entries');             self.entries_collection.each(function(model) {                 model.save();             });             */             self.entries_list = new entrieslist({collection: self.entries_collection});              self.collectionsynced();         });     },     collectionsynced: function() {         this.columnright.show(this.voting_intro);         this.collectionlist.show(this.entries_list);         this.listento(this.voting_intro, 'start:clicked', function() {             this.displayentry(0);         });     },      ... 

i'm using marionette's layout, collectionview, itemview's etc, not sure how critical question.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -