Backbone router.navigate how to pass dynamic ID

徘徊边缘 提交于 2019-12-12 00:44:24

问题


Using backbone marionette I need to navigate to the following route:

 'page/:id': 'page'

This is what I have tried so far:

success: function (page) {
  id = page.get('id')
  router.navigate('page', {trigger: true});
}

But I have two problems with above.

1) Router is undefined in my view 2) I cannot find a reference to how I pass the ID

How do I resolve this or does marionette have any build in methods?


回答1:


You can pass the id just putting it in the url:

success: function (page) {
   id = page.get('id')
   router.navigate('page/' + id, {trigger: true});
}

Reference

Regarding the router you need to create it:

var MyRouter = Backbone.Router.extend({
    routes: {
       'page/:id':     'page'
    },
    page: function(id) {
        ...
    }

});

var router = new MyRouter();


来源:https://stackoverflow.com/questions/20035140/backbone-router-navigate-how-to-pass-dynamic-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!