显示信息栏从侧边栏选择的模型 [英] Display selected model from sidebar in information bar

查看:142
本文介绍了显示信息栏从侧边栏选择的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这对右侧的侧边栏应用程序。显示集合。集合的每个模型都有选择行为。

I'm working on a application which has sidebar on the right side. That displays collection. Each model of the collection has select behavior.

在页面的顶部中间我有一个就像信息栏,这观点应该更新它的信息来源,当我选择在侧边栏的意见一一独立骨干视图

In the top center of the page I have one independant backbone view which acts like "info bar" and this view should update it's infomation when I select one of the views in the sidebar

我该怎么办呢?我认为在侧边栏的每个视图应该入选事件与模型参数,我的问题是我怎么能听在Backbone.js的我的信息栏认为变化?

How can I do it? I think that the each view in the sidebar should have "selected" event with model argument, and my question is how can I listen that change in my info bar view in Backbone.js???

推荐答案

这听起来像是一些会由事件聚合模式来服务好。德里克·贝利发表在这个问题上,你可以在这里阅读一个非常好的文章。

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.

<一个href=\"http://lostechies.com/derickbailey/2012/04/03/revisiting-the-backbone-event-aggregator-lessons-learned/\">Event聚合模式

我伸出我的Backbone.View,使所有的意见都提供给他们的事件聚合对象是这样的。

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);

在选择你的模型视图基本上,你的view.eventAggregator会引发一些自定义事件。

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
}

这是一个pretty有用的模式。只是不要忘记,当你关闭你的MAINVIEW解除绑定事件。 : - )

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

这篇关于显示信息栏从侧边栏选择的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆