Im显示一个CompositeView,其中每个模型都有一个属性,它是图像的url。如果我只是在区域中显示视图,图像尚未完成加载,这看起来不太性感(它们在加载时显示)。Backbone Marionette:在所有图像加载之前不要显示视图
我想在显示视图之前等待所有图像加载完毕。现在,我试图通过推迟图像加载来实现它,但这可能不是正确的方式(也许这应该在模型上完成)。
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);
});
});
}
};
});
你是问,如果这东西在模型或什么办?我认为这种观点应该处理图像加载 – Dethariel 2014-10-27 14:12:33
Naah。模型问题只是一个子问题:) 我想最好的解决方案。 – tolborg 2014-10-27 14:27:06
最好的解决方案是检查是否加载了所有图像,然后渲染您的视图。您可以将该逻辑(图像加载检查)提取到集合。看看[这里](http://stackoverflow.com/questions/18974517/javascript-check-if-images-are-loaded-before-gameloop)和[这里](http://stackoverflow.com/questions/15587330/测试,如果所有的图像被加载) – 2014-10-27 14:35:46