我应该如何让 Backbone 应用程序中的视图相互更改?
我有一个名为 Foo 的 Backbone 视图,它有一个滚动条。当滚动条向某个方向移动时,我希望应用程序中的其他视图以各种方式发生变化。这些视图没有嵌套在 Foo 内部——它们是完全独立的并且独立管理。
我是否应该将对所有其他视图的引用传递到 Foo 的构造函数中,以便它可以手动更新它们?或者我应该使用事件来通知滚动条移动的其他视图?如果是这样,该事件应该由 Foo 触发哪个对象,并由其他视图监听?
这些纯粹是视觉上的变化,没有模型支持它们。
I have a Backbone view named Foo which has a scrollbar. When the scrollbar is moved in a certain direction I want to have other views in my application change in various ways. These views are not nested inside of Foo -- they are completely separate and managed independently.
Should I pass references to all the other views into Foo's constructor so that it can manually update them? Or should I use events to notify these other views of the scrollbar movement? If so, what object should the event be triggered on by Foo, and listened to by the other views?
These are purely visual changes and there is no model backing them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下我会使用事件,
以这个例子为例:
如果您需要更多信息,关于将 1 个视图引用到另一个视图或事件,您还可以仔细阅读 Derick Bailey 的这篇文章,他将上述方法解释为 eventAggregator并列出了有关如何使用它(和替代方案)的记录示例。
http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/
i would use events in this case
take this example:
should you need more info, on either referencing 1 view to another, or events, you can also take a good look at this article by Derick Bailey, he explains the above method as an eventAggregator and lists a documented example on how to work with it (and alternatives).
http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/