Search code examples
javascriptbackbone.jsrequirejs

appRouter is not fired on load


I'm trying to set up a router for my backbone application. However when i go to localhost:8888/extractors or localhost:8888/extractors/new. neither of the console messages are being outputted. How come when it is defined in the router itself?

require(['new-extractor-view', 'extractors-collection', 'backbone'], function (NewExtractorView, ExtractorsCollection) {
    'use strict';

    var extractorCollection = new ExtractorsCollection();

    new NewExtractorView({
        collection: extractorCollection
    });

    //
    // Initialize URL router
    //
    var AppRouter = Backbone.Router.extend({
        routes: {
            'extractors': 'extractorsRoute',
            'extractors/new': 'newExtractorRoute',
            '*actions': 'defaultRoute'
        }
    });

    var appRouter = new AppRouter;


    appRouter.on('route:extractorsRoute', function () {
        console.log('test1')
    });

    appRouter.on('route:newExtractorRoute', function () {
        console.log('test2')
    });


    Backbone.history.start();
});

Solution

  • If you want to use url's without # you should initialize history like:

    Backbone.history.start({ pushState: true })
    

    Read more about the pushState option.