Backbone router not working on page load

与世无争的帅哥 提交于 2020-01-06 07:05:04

问题


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?


回答1:


You haven't define the pageAction function in your router :

var Router = Backbone.Router.extend({
    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
    },

    pageAction: function() { // the missing function
        ...
    }



回答2:


OK, so to answer my own question I was going about this the wrong way, I was using the following route to match all pages and try and make one solution for all:

':page': 'pageAction',

The problem with this being one solution for all pages was knowing which view to instantiate on page load.

Considering this is a small site which wont have a large amount of changing content or new URLs it makes sense to route the individual URLs like so:

'our-approach.html': 'instantiateOurApproach',
'our-work.html': 'instantiateOurWork',
'who-we-are.html': 'instantiateWhoWeAre',
'social-stream.html': 'instantiateSocialStream',
'contact.html': 'instantiateContact'

Now this means I know exactly which view to instantiate and when.

The other functionality which was inside the pageAction function should have been dependent on click event not the routing.

In the end it appears I was trying to do too much with the router and the better option is to only use the router to instantiate views and to let everything else run off click events.

So that's my solution, please comment with any options on if this is right or wrong.



来源:https://stackoverflow.com/questions/22199059/backbone-router-not-working-on-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!