javascript - Backbone Router not triggering route - Stack Overflow

I have the following router:appRouter = Backbone.Router.extend({routes: {'': 'inbox'

I have the following router:

appRouter = Backbone.Router.extend({
  routes: {
    '': 'inbox',
    'inbox': 'inbox',
    'discussions_engagement': 'contacts',
  },

  inbox: function(page) {
    console.log('inbox');
    var page = page || 1;
    engage.app.hydrateInbox(page, engage.app.showInbox);
  },
  ....
};

When I am on http://[...]/#inbox and I call

appRouter.navigate('inbox', {trigger: true});

the inbox action doesn't fire which is what I want to achieve. Now I have looked at the source of Backbone (.js#L1027) and I see that it doesn't support what I'm trying to do but is there some way of acplishing this?

I have the following router:

appRouter = Backbone.Router.extend({
  routes: {
    '': 'inbox',
    'inbox': 'inbox',
    'discussions_engagement': 'contacts',
  },

  inbox: function(page) {
    console.log('inbox');
    var page = page || 1;
    engage.app.hydrateInbox(page, engage.app.showInbox);
  },
  ....
};

When I am on http://[...]/#inbox and I call

appRouter.navigate('inbox', {trigger: true});

the inbox action doesn't fire which is what I want to achieve. Now I have looked at the source of Backbone (https://github./documentcloud/backbone/blob/master/backbone.js#L1027) and I see that it doesn't support what I'm trying to do but is there some way of acplishing this?

Share Improve this question asked Feb 25, 2012 at 21:06 Gavin SchulzGavin Schulz 4,7264 gold badges25 silver badges32 bronze badges 1
  • 1 Not an answer to your question, but maybe a solution (if I get you correctly): (Re-)Render your page / your most parent view, e.g. appView.render(). – SunnyRed Commented Feb 25, 2012 at 21:37
Add a ment  | 

3 Answers 3

Reset to default 4

I would create an event manager in your engage.app object, like this:

var vent = _.extend({}, Backbone.Events);

Then in your router do this for the inbox route:

vent.trigger('inbox:show', page);

And handle that event in the engage.app object, doing the code there that used to be in the route handler.

Now, instead of calling appRouter.navigate you can trigger that same event.

Also, from that handler, you can call appRouter.navigate('inbox'); without passing true. Now you can get your app to the state you want without trying to force the route.

As far as I can tell... As of Backbone 0.9.10 using appRouter.navigate('inbox', {trigger: true}); works as expected.

Another option is just to call the method

appRouter.inbox();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744138222a4560159.html

相关推荐

  • javascript - Backbone Router not triggering route - Stack Overflow

    I have the following router:appRouter = Backbone.Router.extend({routes: {'': 'inbox'

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信