I faced the problem that view switches with router.
My application is written with Backbone.js. It has 2 views, ApplicationView
and ApplicationSubView
.
Originally, I thought that if occurred click event then through router should move the page
For example, anyone who clicked the element having the about
class then must exprience moved and re-rendered pages
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();
});