Backbone router

I am trying to update the url, but not navigate the page when using tabs in the trunk, so that the tab can be bookmarked, when I run it from my codebase, it does nothing.

app.Router.navigate('groups/teams', { trigger:false });

      

however if i run it in console it changes url from http: //app.local/groups ' to ' http: //app.local/groups#groups/teams ' why does it add #

? I would like to update the url: http: //app.local/groups/teams '.

Am I using my router incorrectly? My router is pretty simple,

        var app = app || {};

        var Router = Backbone.Router.extend({

        routes: {

            'groups' : 'groups'

        },

        groups: function() {
             alert("Groups");
        }

    });

    app.Router = new Router();

      

+3


source to share


3 answers


Usually when you have a url:

www.example.com/foo

      

the foo part is considered to point to a resource called "foo" on example.com. So, if the client (ie Javascript code) needs to redirect the user to "www.example.com/bar", then it can't do anything, because the browser will take the user to a new page entirely (with brand new Javascript code) ...

Most, but not all, modern browsers have implemented a pushstate function that wraps around this and allows the client to set a URL to "www.example.com/bar" without having the browser go back to the server and ask for this page. However, since not all browsers support pushstate, Backbone does not use it by default.



Instead, Backbone uses an old piece of web technology: bind / hash (e.g. www.example.com/#foo). If you don't want this to be done, then you need to pass parameters pushState: true, hashChange: false

when your router starts up. However, even if you do this, Backbone will still fallback to hash-based navigation if the browser does not support pushstate, so there is no guarantee it will not do "#bar" yet.

My advice would be to adapt to have all your client routes are hash-based, but if you are confident that all of your users will be using push-state browsers, you can use this approach. Click-state browsers can be found here:

http://caniuse.com/#search=pushstate

Hope it helps.

0


source


hash fragments (#page) were used to provide these permalinks, but with the advent of the history API

just set pushState ture:



Backbone.history.start({pushState: true, root: "/public/search/"})

      

0


source


Add this code to your router initialize: function

 $('body').delegate('a[href]:not([href^=\#])', 'click', function (e) {
       e.preventDefault();
   Backbone.history.navigate($(this).attr('href'), {trigger: true});

      

});

it works in my case

0


source







All Articles