在主干中从一个视图向另一个视图触发事件 [英] fire an event from one view to another in backbone
问题描述
我有一个主视图,在该主视图中,我有另一个视图,当我单击按钮时会创建该视图.有没有办法在父视图上侦听从子视图触发的事件的自定义事件.我尝试使用 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.
推荐答案
是的,没问题...您会想要使用事件聚合器"模式.这是 Backbone 中的 1 行代码:
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.
我经常使用它!
我还在这里写了更多关于它的博客: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屋!