Backbone.js:视图之间的通信 [英] Backbone.js: communication between views

查看:21
本文介绍了Backbone.js:视图之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含条目列表的 Backbone 应用程序,很像示例应用程序 Todos (

在您的情况下,我建议使用我在该文章中描述的事件聚合器.您可以让每个项目视图侦听editmode"事件或类似事件.当此事件触发时,侦听它的每个视图都会更新自身以进入编辑模式.那么当你点击完成"时你会做相反的事情 - 发送一个viewmode"事件或类似的东西,并让每个视图适当地更新自己.

I'm working on a Backbone app that contains a list of entries, much like the example app Todos (http://documentcloud.github.com/backbone/examples/todos/index.html).

So, I have an App view and one view per list item. Now, say I have a global edit button. The App view would handle a click and what I then want to do is tell each list view to show a delete button.

In the screenshots below (from Spotify), pressing the Edit button causes all list views to change appearance.

What's the best way to do this with Backbone. I need to iterate over all list views and call a editMode function. But the App view (out of the box) doesn't know about the list views..

解决方案

I wrote up an article a while back on a few different options for coordinating between views: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

in your case, i'd recommend using the event aggregator that i describe in that article. you could have each item view listen for a "editmode" event, or something similar. when this event fires, each view that listened for it would update itself to go into edit mode. then you would do the opposite when you click "done" - send a "viewmode" event, or something similar, and have each view update itself appropriately.

这篇关于Backbone.js:视图之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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