触发一个事件从一个视图到另一个骨干 [英] fire an event from one view to another in backbone

查看:99
本文介绍了触发一个事件从一个视图到另一个骨干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主视图和主视图里面我有,当我点击一个按钮时生成另一种看法。有没有侦听为从子视图触发的事件父视图的自定义事件的方法。我试着使用jQuery触发通过EL财产做,但没有相当的工作。

I have a main view and inside that main view I have another view that gets created when I click a button. Is there a way to listen for a custom event on the parent view for an event that is fired from the child view. I tried to do it through the el property using jQuery trigger but that didn't quite work.

推荐答案

雅,没问题...你会希望使用事件聚合的格局。这是1号线code的骨干:

Ya, no problem... you'll want to use the "Event Aggregator" pattern. It's 1 line of code in Backbone:

VAR eventAgg = _.extend({},Backbone.Events);

现在,你可以在你的应用程序从该对象触发/绑定事件,无处不在,让您的应用程序的不同部分互相以分离的方式进行通信。

Now can you trigger / bind to events from this object, everywhere in your app, and have the different parts of your app communicate with each other in a decoupled manner.

我用这个有很多!

我也博客更多关于它在这里:<一href=\"http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/\">http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

I also blogged more about it here: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

这篇关于触发一个事件从一个视图到另一个骨干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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