Frage

I'm trying to deal with backbone.js pushState for change hashtags on pretty urls. Using localhost and the path to my script is http://localhost/test/backbone/test.html. But each time the click is throwing me to the localhost/login. What am I doing wrong?

var AppRouter = Backbone.Router.extend({
    routes: {
        "login": "getPost",
        "*actions": "defaultRoute"
    }
});

var app_router = new AppRouter;

app_router.on('route:getPost', function (id) {
    alert( "login" );   
});

app_router.on('route:defaultRoute', function (actions) {
    alert( actions ); 
});

app_router.navigate("/login", {trigger: true});
Backbone.history.start({pushState: true, root: '/login/'});
War es hilfreich?

Lösung

You need add:

$(document).delegate("a", "click", function(evt) {
    var href = $(this).attr("href");
    var protocol = this.protocol + "//";

    if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' &&      href.substring(0, 1) !== '#') {
        evt.preventDefault();

        Backbone.history.navigate(href, true);
    }
});

And last 2 strings should be:

app_router.navigate("/login", {trigger: true}); // <- should remove this string
Backbone.history.start({pushState: true, root:"test/backbone/test.html"});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top