メニュー付きのメイン ビュー (IndexView)、HTML5 ビデオ ループ、およびコンテンツ用の div (#container) で構成される Backbone.JS を使用してアプリを開発しています。アプリが初期化されると、ルートに応じてビューがレンダリングされ、#container 要素に表示されるという考え方です。ルートに関係なく、IndexView は常に表示される必要があります。これは私が持っているものです:
ルーター.js:
var initialize = function () {
// The IndexView will be rendered when the app is initialized
console.log("Rendering index view...");
var indexView = new IndexView();
indexView.render();
var app_router = new AppRouter;
// One of the routes
app_router.on('route:about', function () {
var aboutView = new AboutView();
aboutView.render();
});
// Other routes here…
Backbone.history.start();
};
return {
initialize: initialize
};
ビュー/index.js:
define([
'jquery',
'underscore',
'backbone',
'text!templates/index.html'
], function ($, _, Backbone, indexTemplate) {
var IndexView = Backbone.View.extend({
el : $("body"),
render : function () {
var data = {};
var compiledTemplate = _.template(indexTemplate, data);
this.$el.html(compiledTemplate);
}
});
return IndexView;
});
ビュー/about.js:
define([
'jquery',
'underscore',
'backbone',
'text!templates/about.html'
], function ($, _, Backbone, aboutTemplate) {
var AboutView = Backbone.View.extend({
el : $("#container"),
render : function () {
var data = {};
var compiledTemplate = _.template(aboutTemplate, data);
this.$el.html(compiledTemplate);
}
});
return AboutView;
});
問題は、IndexView は適切にレンダリングされますが、他のビューは正しくレンダリングされないことです。何らかの理由で、IndexView によって作成された #container 要素が表示されないためだと思われます。私がこれを言うのは、代わりにこれらのビューを body 要素にレンダリングする場合、それらはそれをうまく行うからです。
助言がありますか?前もって感謝します!