Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone pushState routes

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/'});
like image 336
Nobodyisperfect Avatar asked Jun 07 '26 17:06

Nobodyisperfect


1 Answers

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"});
like image 191
SergeevDMS Avatar answered Jun 10 '26 06:06

SergeevDMS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!