0
我遇到了使用路由器切換視圖的問題。如何在backbone.js中使用路由器切換視圖
我的應用程序是用Backbone.js編寫的。它有2意見,ApplicationView
和ApplicationSubView
。
原來我以爲,如果發生點擊事件,然後通過路由器應該移動至頁面
例如,誰點擊了具有about
類的元素,則必須先進經驗移動和重新渲染頁面
var app = app || {};
$(function() {
'use strict';
var ApplicationView = Backbone.View.extend({
//bind view to body element (all views should be bound to DOM elements)
el: $('body'),
//called on instantiation
initialize: function() {
//set dependency on ApplicationRouter
this.router = app.Router;
this.subView = new ApplicationSubView();
//call to begin monitoring uri and route changes
Backbone.history.start();
},
showSpinner: function() {
console.log('show the spinner');
},
hideSpinner: function() {
console.log('hide the spinner');
},
loadSubView: function() {
this.showSpinner();
var subView = new SubView();
subView.on('render', this.hideSpinner);
}
});
var ApplicationSubView = Backbone.View.extend({
events: {
'click ul.pills li.home-pill a': 'displayHome',
'click ul.pills li.about-pill a': 'displayAbout',
'click ul.pills li.contact-pill a': 'displayContact'
},
displayHome: function() {
this.trigger('render');
console.log('a subView render');
this.router.navigate("home", true);
return this;
},
displayAbout: function() {
this.trigger('render');
console.log('a subView render');
this.router.navigate("about", true);
return this;
},
displayContact: function() {
this.trigger('render');
console.log('a subView render');
this.router.navigate("contact", true);
return this;
}
});
//load application
app.view = new ApplicationView();
});
太感謝你了!我不寫英文好..所以你很難理解我的問題,但是,你非常好心的回答我試試吧! –