简体   繁体   中英

Backbone.js click event handler : trigger route with parameter

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM