Reputation: 984
I want to change the current page of my backbone application by changing the url. I have the routes setup and when I change the url, my pages are changing, expect everytime I visit a given route a new view is rendered to the dom.
My router :
myApp.Application = Backbone.Router.extend({
routes: {
'' : 'home'
//etc
},
initialize : function() {
myApp.dom.$container = $('#container');
myApp.dom.$container.empty();
},
home : function() {
myApp.loginView = new myApp.views.home();
myApp.dom.$container.append(myApp.home.render().el);
}
})
Am I right in thinking that I need to check to see if (for example) myApp.loginView exists before I render it?
So something like :
home : function() {
if (!myApp.loginView) {}
myApp.loginView = new myApp.views.home();
myApp.dom.$container.append(myApp.home.render().el);
} else {
//just show it
}
}
..and do this for each of my routes?
Thanks!
Upvotes: 1
Views: 2347
Reputation: 6267
This seems wet, I would rather do something like call isAlreadyRendered()
Also, sticking everything to myApp.SpecificName, when your myApp.views seem to be classes seems wrong.
What about initializing with App.views.class = new App.Views.Class
then create a factory for starting views, like
function factory(className){
if( App.views[className.toCamelCase()] ) return;
App.views[className.toCamelCase()] = new App.Views[className].apply(App.Views, [].slice.call(arguments, 1))
}
something like th at, I'm used to using coffeescript which would make things easier as well.
the apply and slice and all that is so that you should be able to pass in as many args as you want into factor after the first one, though this is all from my head so it might be a learning experiment to get it to work and understand the joys of using apply and call ( which if you use coffeescript you will be even happier )
enjoy!
Upvotes: 2