0

My app's index page is at http://cms/admin (I'm on localhost). On the index page there is only one a element:

<a href="/admin/test">deneme</a>

When i click on the link it goes to /cms/admin/test

I want to use BackboneJS's routing mechanism to convert my app to ajax friendly app but i can't do it until now. Here is my JS code:

$(function() {
    var AppRouter = Backbone.Router.extend({
        routes: {
            "test": "defaultRoute"
        },

        defaultRoute: function() {
            console.log('its here');
        }
    });

    var appRouter = new AppRouter();

    Backbone.history.start({
        pushState: true,
        slient: true,
        root: '/admin/'
    });
});

When i run the page and click the link, it doesn't log anything to console and browser follows the link. After page loads, it logs "its here" message.

I already tried it without the root param, "/admin/test" instead of "test". i tried every combination of: "test", "/test", "test/", "/admin/test", "admin/test" etc..

Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
cnkt
  • 2,943
  • 5
  • 30
  • 43

2 Answers2

3

You have to override the default behavior of the link.

You have to move between pages explicitly calling appRouter.navigate("test", {trigger: true});. So try to capture the click event over your link within a View.events, prevent the default behavior of the link and call appRouter.navigate()

Update

Also you can do it this in a batch:

$("a.bb_link").each( function( index, link ){
  $(link).click( function( event ){
    event.preventDefault();
    appRouter.navigate( $(this).attr( "href" ), {trigger: true} );
  });
});​

Every link with class bb_link will be using the Backbone.Router.

fguillen
  • 36,125
  • 23
  • 149
  • 210
  • There's also a good example of this in Tim Branyen's backbone-boilerplate using a `data-bypass` attribute: https://github.com/tbranyen/backbone-boilerplate/blob/master/app/main.js#L22 – Chris Salzberg Aug 20 '12 at 23:50
1

Try setting your anchor tag as follows:

<a href="#admin/test">deneme</a>

Note the hash (#)

Greg Ross
  • 3,479
  • 2
  • 27
  • 26