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