Here we use on and off to listen for route events, not once , because we cannot rely on a single event that is not the current route. When we get a route, even if it is not our current route, we can destroy the view and remove the listener:
// Destroy view var self = this; var onRoute = function(route, params){ if(route !== 'sample'){ demoView.remove(); self.off('route', onRoute); } }; this.on('route', onRoute);
I changed your test script here: http://jsfiddle.net/rgthree/hcuX9/3/
Another option, since your violin (and not in your question) goes directly to another species. This causes another route event to fire after sample2 route. Because of this, the above will delete the view. Now it is much more complete. The hacky way you could handle this is to simply set aside once in setTimeout so that it is not tapped until the current route is launched:
// Destroy view var self = this; setTimeout(function(){ self.once('route', function(){ demoView.remove(); }); }, 0);
You can see your script using this method here: http://jsfiddle.net/rgthree/hcuX9/4/
source share