Display selected model from sidebar in information bar

不问归期 提交于 2019-12-01 11:05:45

This sounds like something that would be served well by an event aggregator pattern. Derick Bailey posted a really nice article on this matter which you can read here.

Event Aggregator Pattern

I extended my Backbone.View so that all views have the event aggregator object available to them like this.

Backbone.View.prototype.eventAggregator = _.extend({}, Backbone.Events);

Basically when your model view is selected, your view.eventAggregator will trigger some custom event.

sidebarView.eventAggregator.trigger('selected', this.model);

or something of this sort. With Backbone events, you can pass parameters through the trigger function to your event listener. In the above example I passed the model with the specific event.

In your main view, you'd be listening for this event.

mainView.eventAggregator.on('selected', myFunction, this);

myFunction: function(model) {
    // Some code to execute - model is available through param
}

It's a pretty useful pattern. Just don't forget to unbind the event when you close out your mainView. :-)

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