在主干中将事件从一个视图触发到另一个视图

发布于 2024-12-11 06:10:22 字数 108 浏览 0 评论 0 原文

我有一个主视图,在该主视图内有另一个视图,当我单击按钮时会创建该视图。有没有办法在父视图上侦听从子视图触发的事件的自定义事件。我尝试使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糖 2024-12-18 06:10:22

是的,没问题...您将需要使用“事件聚合器”模式。 Backbone 中只有 1 行代码:

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

现在,您可以在应用程序中的任何位置触发/绑定到来自此对象的事件,并拥有应用程序的不同部分以解耦的方式相互通信。

我经常用这个!

我还在这里发布了更多相关信息:http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

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.

I use this a LOT!

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/

情绪操控生活 2024-12-18 06:10:22

这是另一种方法,对于正在寻找一种简单方法将事件从子视图“冒泡”到父视图的人来说可能更直观:

https://github.com/dgbeck/backbone.courier

自述文件中有一些关于它与事件聚合器的比较的讨论。

Here is another approach, that might be more intuitive for people who are looking for a simple way to "bubble" events up from a child view to a parent view:

https://github.com/dgbeck/backbone.courier

There is some discussion in the readme on how it compares to event aggregators.

像你 2024-12-18 06:10:22

不需要滚动您自己的“事件聚合器”或中介器或 pubsub(就像上面描述的那样简单),因为 Backbone 将 Events 对象直接混合到其命名空间中的 Backbone 对象。这是源代码及其实现文档的链接

允许 Backbone 对象充当全局事件总线,适合那些希望在方便的地方进行全局“pubsub”的人。

Rolling your own "event aggregator" or mediator or pubsub (as easy as it is described above) is not necessary since Backbone mixes in the Events object directly to the Backbone object in their namespace. Here's a link to the source code and their documentation on the implementation.

Allow the Backbone object to serve as a global event bus, for folks who want global “pubsub” in a convenient place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文