Redirects

Normally users traverse a site by clicking on anchor tags, better known as hyperlinks. The same is true for Backbone-powered sites; even if you use hash-based routes you can still create normal hyperlinks for them:

<a href="#foo">Click here to go to the "foo" route</a>

Sometimes however you will want to move the user to a different route using JavaScript instead. For instance, you might have a Submit button on a page and after it triggers a save of the page's Model you want it to redirect the user to a different route. This can be done by using the Routers navigate method, like so:

var router = new Backbone.Router({
    routes: {
        foo: function() {
            alert('You have navigated to the "foo" route!'),
        }
    }
});
router.navigate('foo', {trigger: true});

It's important to note the addition of the trigger option as the second argument to navigate. Without this extra argument, Backbone will take the user to the route's URL but won't actually trigger the route's logic.

The navigate method can also take one other option: replace. If this option is provided, Backbone will navigate to the specified route but will not add an entry to the browser's history. Take an example of the following line of code:

router.navigate('bar', {replace: true, trigger: true});

Not creating a history entry means that, for instance, if the user visits another page and then clicks on their browser's Back button instead of going back to the replaced page, they will be taken to the page before it in the browser's history. Since this sort of behavior will likely be confusing for your user, it is recommended that you limit the use of replace: true to routes that are temporary, such as the route for loading a page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.141.44