Reputation: 121
Hi I am struggling through working with backbone routers, and I am wanting to find out about instantiating routers.
Can I have one routers file in my app where all the routes are defined, but I make an instance of the router in my appView and in my appModel. I am doing this so that I can call the router from both the view and the model, however I feel that this might be a rather sloppy way to do this?
Is there a better way?
Upvotes: 0
Views: 46
Reputation: 513
The best way to handle this that has worked for me in the past is to create a global javascript file that is included on every page:
<script type="text/javascript" charset="utf-8">
window.APP = {};
APP.Routers = {};
</script>
Render the router once on page load:
APP.Routers['my_router_name'] = new MyRouterName({});
Now you can access it anyplace throughout the app by
APP.Routers.my_router_name
I tend to follow this pattern with not only routers, but also we collections, models, etc. also.
<script type="text/javascript" charset="utf-8">
window.APP = {};
APP.Routers = {};
APP.Collections = {};
APP.Models = {};
</script>
This solves the problem you have stated here it seems...."I am doing this so that I can call the router from both the view and the model".
Upvotes: 1