Question

I want to redirect the user to another page when they click on a button. Here's the code:

template:

<button type="button" id="edit-button">Edit</button>

router:

App.Router.Accounts = Backbone.Router.extend({
    routes: {
        'accounts/:accountID/audience/edit' : 'editAudience'
    }
});

view:

router: new App.Router.Accounts(),

events: {
    "click #edit-button": "redirectToEdit"
},

redirectToEdit: function(){
    url = "accounts/"+this.account_id+'/audience/edit';
    this.router.navigate(url, {
        trigger: true,
        replace: true
    });
}

But I get the error:

Uncaught TypeError: Cannot use 'in' operator to search for 'model' in foo123

where foo123 is the account id.

What am I doing wrong?

Was it helpful?

Solution

If you want to change the URL you can use the Router.navigate method:

App.Router.navigate(url, {
    // trigger: true/false,
    // replace: true/false
});

Note that you are missing a / in your string concatenation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top