I have a Compositeview that represents a list of tasks with a header that contains a counter for the tasks in such list.
I have defined an ui hash for the counter to deal with it and to properly initialize it:
The view:
ui: {
counter: "#counter",
},
onRender: function(){
this.ui.counter.text(this.collection.length);
}
The problem I have is that the collection.length is SOMETIMES 0 by the time the collection is rendered. The task models are rendered though. By sometimes, I mean that sometimes the collection.length will have the proper value and sometimes not. This looks like a race condition. However, I make sure the collections are fetched before even creating the views and obviously, before showing them.
A simplified example of my controller looks like:
showTasks: function(){
//layout
var kanbanLayout = new View.Layout();
//tasks
var backlogFetch = App.request("backlog:task:entities");
$.when(backlogFetch).done(function(backlogTasks){
var backlog = new View.Column({
collection: backlogTasks,
});
kanbanLayout.on("show", function(){
kanbanLayout.backlog.show(backlog);
});
App.once("fetched:kanban", function(){
App.mainLayout.mainRegion.show(kanbanLayout);
});
}
}
As you can see, I'm waiting for the data to be fetched, but apparently the code is executed before the data has been fetched. Any ideas of what am I doing wrong?
The full code of my:
- controller: https://github.com/mezod/multikanban/blob/master/app/scripts/apps/kanban/show/show_controller.js#L40
- view: https://github.com/mezod/multikanban/blob/master/app/scripts/apps/kanban/show/show_view.js#L421 can be seen here.
As you can see, it is strongly following David Sulc's structure: https://github.com/davidsulc/structuring-backbone-with-requirejs-and-marionette/blob/master/assets/js/apps/contacts/show/show_controller.js#L13
App.request()
really returnsDeferred
? – hindmost