I have a backbone application which works great, except for the routing on page load. I need to use the router to instantiate the correct view on page load, but for some reason the functions wont fire.
Here is my code:
var Router = Backbone.Router.extend({
routes: {
':page': 'pageAction', // Igonre this route - this fires successfully after an AJAX call
'our-approach.html': 'instantiateOurApproach',
'our-work.html': 'instantiateOurWork',
'who-we-are.html': 'instantiateWhoWeAre',
'social-stream.html': 'instantiateSocialStream',
'contact.html': 'instantiateContact'
},
instantiateOurApproach: function() {
var our_approach_view = new OurApproachView();
},
instantiateOurWork: function() {
var our_work_view = new OurWorkView();
},
instantiateWhoWeAre: function() {
var who_we_are_view = new WhoWeAreView();
},
instantiateSocialStream: function() {
var social_stream_view = new SocialStreamView();
},
instantiateContact: function() {
var contact_view = new ContactView();
}
});
var router = new Router();
Backbone.history.start({pushState: true, root: "website/url/"}); // LOCAL URL
I am unsure if the problem is related to the fact that I have pushState: true
, or if I am just doing something wrong altogether, I am quite new to backbone so any help and explanation would be great.
Cheers.
UPDATE
So just to make things a little clearer, here are the following URLs that will be in use:
http://www.website.com/our-approach.html
http://www.website.com/our-work.html
http://www.website.com/who-we-are.html
http://www.website.com/social-stream.html
http://www.website.com/contact.html
All of these pages will be using the same HTML and JS code, the only difference on each page will be the HTML inside a defined containing element.
For example when I navigate to http://www.website.com/our-approach.html
I need the router to trigger for our-approach.html
and run the function instantiateOurApproach
and so on for the other URLs.
UPDATE
OK so I have figure out my problem, my initial route:
':page': 'pageAction', // Igonre this route - this fires successfully after an AJAX call
Does not work on page load, but matches absolutely any URL, so in the order that I have them above the pageAction
function always runs meaning that the view instantiation function never runs.
If I swap the order round like so:
routes: {
'our-approach.html': 'instantiateOurApproach',
'our-work.html': 'instantiateOurWork',
'who-we-are.html': 'instantiateWhoWeAre',
'social-stream.html': 'instantiateSocialStream',
'contact.html': 'instantiateContact'
':page': 'pageAction', // Igonre this route - this fires successfully after an AJAX call
}
The view instantiates correctly, but not my PageAction
function doesn't run, is there any way to have both functions run?