Reputation: 12179
I have several views which act as main view modules (RequireJS) that correspond to routes. I plan on using these main view modules as entry/controllers which initialise more sub views. Each route requires most views active on the page to be removed along with the sub views before new views can be initialised. I'm lost on how to implement this though. Should I keep track of view loaded and do the cleaning up in the AppRouter or main view modules?
My routes
var AppRouter = Backbone.Router.extend({
routes: {
'pagex/new': 'newPost',
'pagey/:id: 'foo'
},
newPost: function() {
// remove and cleanup current active views here (I don't know how)
// render newPost view
},
// ...
});
The main view for entire application:
var appView = Backbone.View.extend({
var router = new Router();
Backbone.history.start({ pushState: true });
$(document).on('click', 'a:not([data-bypass])', function (evt) {
var href = $(this).attr('href');
var protocol = this.protocol + '//';
if (href.slice(protocol.length) !== protocol) {
evt.preventDefault();
router.navigate(href, true);
}
});
});
Upvotes: 0
Views: 106
Reputation: 13058
Save yourself some time, and use Marionette. Then you'll define your region(s), in which you will show your views. Marionette will handle closing of the views for you (and it has some many other goodies, so there's simply no excuse not to use it).
Upvotes: 1