Skip to content

Commit 26156c2

Browse files
committed
Wait for the data to load before rendering
When setting the version, that can trigger an asynchronous request to download the json file. We were assuming the new data was set when the function returned, which meant on a new load, we were not rendering the right version, but whatever happened to be loaded before. Use a callback to render the views only once the new data has been downloaded and set (or immediately if we're already in the right version).
1 parent c878f2a commit 26156c2

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

site/js/docurium.js

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,8 @@ $(function() {
316316
var MainListView = Backbone.View.extend({
317317
template: _.template($('#index-template').html()),
318318

319-
initialize: function() {
320-
this.listenTo(this.collection, 'reset', this.render)
321-
},
322-
323319
render: function() {
324320
this.el = this.template({groups: this.collection.toJSON()})
325-
this.trigger('redraw')
326321
return this
327322
},
328323
})
@@ -554,16 +549,27 @@ $(function() {
554549
})
555550
},
556551

557-
setVersion: function (version) {
552+
setVersion: function (version, success) {
558553
if(!version) {
559554
version = _.first(docurium.get('versions'))
560555
}
556+
557+
current = docurium.get('version')
558+
if (current == version) {
559+
if (success)
560+
success();
561+
return;
562+
}
563+
561564
docurium.set({version: version})
565+
p = this.loadDoc()
566+
if (success)
567+
p.then(success)
562568
},
563569

564570
loadDoc: function() {
565571
version = this.get('version')
566-
$.getJSON(version + '.json').then(function(data) {
572+
return $.getJSON(version + '.json').then(function(data) {
567573
docurium.set({data: data})
568574
})
569575
},
@@ -647,40 +653,50 @@ $(function() {
647653
},
648654

649655
main: function(version) {
650-
this.doc.setVersion(version)
651-
var view = new MainListView({collection: this.groups})
652-
this.mainView.setActive(view)
656+
var self = this
657+
this.doc.setVersion(version, function() {
658+
var view = new MainListView({collection: self.groups})
659+
self.mainView.setActive(view)
660+
})
653661
},
654662

655663
group: function(version, gname) {
656-
this.doc.setVersion(version)
657-
var group = this.doc.getGroup(gname)
658-
var fdata = this.doc.get('data')['functions']
659-
var cdata = this.doc.get('data')['callbacks']
660-
var version = this.doc.get('version')
661-
var view = new GroupView({group: group, functions: fdata, callbacks: cdata, version: version})
662-
this.mainView.setActive(view)
664+
var self = this
665+
this.doc.setVersion(version, function() {
666+
var group = self.doc.getGroup(gname)
667+
var fdata = self.doc.get('data')['functions']
668+
var cdata = self.doc.get('data')['callbacks']
669+
var version = self.doc.get('version')
670+
var view = new GroupView({group: group, functions: fdata, callbacks: cdata, version: version})
671+
self.mainView.setActive(view)
672+
});
663673
},
664674

665675
groupFun: function(version, gname, fname) {
666-
this.doc.setVersion(version)
667-
var model = new FunctionModel({docurium: this.doc, gname: gname, fname: fname})
668-
var view = new FunctionView({model: model})
669-
this.mainView.setActive(view)
676+
var self = this
677+
this.doc.setVersion(version, function() {
678+
var model = new FunctionModel({docurium: self.doc, gname: gname, fname: fname})
679+
var view = new FunctionView({model: model})
680+
self.mainView.setActive(view)
681+
})
670682
},
671683

672684
showtype: function(version, tname) {
673-
this.doc.setVersion(version)
674-
var model = new TypeModel({docurium: this.doc, typename: tname})
675-
var view = new TypeView({model: model})
676-
this.mainView.setActive(view)
685+
var self = this
686+
this.doc.setVersion(version, function() {
687+
var model = new TypeModel({docurium: self.doc, typename: tname})
688+
var view = new TypeView({model: model})
689+
self.mainView.setActive(view)
690+
})
677691
},
678692

679693
search: function(version, query) {
680-
this.doc.setVersion(version)
681-
var view = new SearchView({collection: this.search})
682-
$('#search-field').val(query).keyup()
683-
this.mainView.setActive(view)
694+
var self = this
695+
this.doc.setVersion(version, function() {
696+
var view = new SearchView({collection: self.search})
697+
$('#search-field').val(query).keyup()
698+
self.mainView.setActive(view)
699+
})
684700
},
685701

686702
changelog: function(version, tname) {
@@ -689,8 +705,9 @@ $(function() {
689705
if (this.changelogView == undefined) {
690706
this.changelogView = new ChangelogView({model: this.doc})
691707
}
692-
this.doc.setVersion()
693-
this.mainView.setActive(this.changelogView)
708+
this.doc.setVersion(undefined, function() {
709+
this.mainView.setActive(this.changelogView)
710+
})
694711
},
695712
});
696713

0 commit comments

Comments
 (0)