【发布时间】:2014-12-22 17:15:22
【问题描述】:
我正在显示一个 CompositeView,其中每个模型都有一个属性,该属性是图像的 url。如果我只是在一个区域中显示视图,图像还没有完成加载,这看起来不太性感(它们在加载时显示)。
我想等到所有图像都加载完毕,然后再显示视图。现在,我试图通过延迟图像加载来使其工作,但这可能不是正确的方法(也许这应该在模型上完成?)。
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);
});
});
}
};
});
【问题讨论】:
-
您是在问这是要在模型中完成的事情还是什么?我认为该视图应该处理图像加载
-
不。模型问题只是一个子问题:) 我想要最好的解决方案。