次のビューとルーターのセットがあります。ページが最初に表示されたときに、「初期化」メソッドでナビゲートされた 'メソッドが呼び出されますが、要素は更新されません。別のビュー(hashbang)にナビゲートしてから戻ると、最初のビューが表示されます。ページが最初にレンダリングされたときに、バックボーン・ビューが表示されない
誰かが間違っていることを知っていますか?
var AppRouter = Backbone.Router.extend({
contentView: null,
routes: {
'welcome': 'welcomeAction',
'settings': 'settingsAction',
'books': 'booksAction',
'book/:id': 'bookAction'
},
initialize: function() {
this.navigate('welcome', true);
},
welcomeAction: function() {
this.contentView = new views.WelcomeView({ 'model': new models.Welcome() });
},
settingsAction: function() {
this.contentView = new views.SettingsView({ 'model': new models.Settings() });
},
booksAction: function() {
this.contentView = new views.BooksView({ 'model': new models.Books() });
},
bookAction: function (id) {
this.contentView = new views.BookView({ 'model': new models.Book({ 'Id': id }) });
}
});
function App() {
this.router = null;
this.initialize = function() {
this.router = new AppRouter;
Backbone.history.start();
};
};
var reader = new App;
reader.initialize();
ビューを以下に示します。
WelcomeView: Backbone.View.extend({
'el': '#content-pane',
tagName: 'div',
template: _.template(templates.welcome),
events: {
},
initialize: function() {
_.bindAll(this, 'render');
this.renderLoading();
this.model.fetch({ success: _.bind(function() {
this.model.set({ Date: new Date() });
this.render();
}, this)
});
},
renderLoading: function() {
var html = _.template(templates.loading)();
$(this.el).empty();
$(this.el).append(html);
},
render: function() {
var html = this.template({ date: this.model.get('Date') });
$(this.el).empty();
$(this.el).append(html);
}
}),
なぜあなたは 'el'と' tagName'を同時に持っていますか? 'el'を定義するとき、既存の要素を参照する必要があり、' el'が定義されていない場合、 'tagName'、' className'および 'id'プロパティーが新しいものを作成するために使われます。 '#content-pane'が既に存在する場合は' tagName'を削除し、そうでない場合は 'tagName'と' id'を使用してください。 – kubetz
'.empty()'の代わりに '.html(html)'を使用してください。 '.append(html)'を実行します。 – kubetz
アドバイスありがとうございました。私はそれほど良く分かりませんでした。 – AwkwardCoder