Im displaying a CompositeView, in which each model has a property which is an url for an image. If I just .show the view in a region the images have not finished loading, which doesnt look very sexy (they are displayed when they have loaded).
I would like to wait until all images have loaded, before I display the view. Right now, Im trying to make it work by deferring image loads, but this is probably not the right way (perhaps this should be done on the model?).
applications/list_view.js
App.module("ApplicationsApp.List", function(List, App, Backbone, Marionette, $, _){
List.Application = Marionette.ItemView.extend({
tagName: 'li',
template: Templates.applications.application
});
List.Applications = Marionette.CompositeView.extend({
className: "applications",
template: Templates.applications.applications,
childView: List.Application,
childViewContainer: "ul"
});
});
applications/list_controller.js
App.module("ApplicationsApp.List", function(List, App, Backbone, Marionette, $, _) {
List.Controller = {
listApplications: function() {
// Set layout
App.trigger("set:layout:authenticated");
// Fetch the applications
var fetchingApplications = App.request('application:entities');
$.when(fetchingApplications).done(function(applications) {
var applicationsListView = new List.Applications({
collection: applications
});
var deferreds = [];
applications.each(function(application) {
deferreds.push(function() {
var loader = new Image();
loader.onload = function() {
console.log(application.get("image_path"));
};
loader.src = application.get("image_path");
});
});
$.when.apply($, deferreds).then(function() {
App.layout.mainRegion.show(applicationsListView);
});
});
}
};
});