Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok lets simplify what you are trying to achieve:</p> <ul> <li>Go to 'News' route, add additional menu</li> <li>Move away from 'News' route, remove the news sub-menu</li> </ul> <p>Now in vanilla Backbone, the only way to do is to do it manually. Something like this:</p> <pre><code>var Router = Backbone.Router.extend({ onBeforeRoute: function () { if (this.subMenuView) this.subMenuView.$el.empty(); // anything else you need to do }, ... //Default route. index: function () { this.onBeforeRoute(); var self = this; }, about: function() { this.onBeforeRoute(); this.aboutView = new AboutView({el:'#topContent'}).render(); }, news: function() { this.onBeforeRoute(); this.newsView = new NewsView({el:'#topContent'}).render(); this.subMenuView = new SubMenuView({el:'#subMenu', collection:this.subMenuCollection}).render(); }, ... } </code></pre> <p>Check out <a href="http://backbonejs.org/#View-remove" rel="nofollow">the docs here</a> to see about the remove method.</p> <p>One other thing I would suggest, is instantiating your views in your initialize method. If you keep recreating them every time a route is fired, the old views hang around in memory and are still bound to the DOM, causing strange issues if your views are bound to data etc. I can see you have something happening to stop this in the <code>close</code> method you have implemented, but it looks like you're not actually calling it anywhere.</p> <p>Also, I would say it is frustrating when you first try to get to grips with Backbone, but stick with it, and keep reading articles on best practices etc, cause you always need to remember that Backbone IS NOT A FRAMEWORK! It is a very simple library, it offers nothing really in the way of conventions or ways to structure your application (which is why many people like it). I love Backbone but I actually use it along with <a href="http://marionettejs.com" rel="nofollow">Marionette</a>, which provides a lot of nice ways to structure your application. I recommend checking it out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload