Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js: communication between views

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..

enter image description here

like image 266
Sebastian Avatar asked Sep 20 '11 13:09

Sebastian


People also ask

What are views in Backbone JS?

The Backbone. js Views specify how your data looks like. They represent model's data to the users. They can be used with any JavaScript template library.

What is the architecture of backbone JS?

It retrieves and populates the data. Models consist of data of an application and logic of the data and represents basic data object in the framework. Models also represent business entities with some business logic and business validations. Its main usage is data storage and business logic.

What is backbone JS give its features?

Backbone. js allows developers to develop one page applications and front-end much easier and better using JavaScript functions. Backbone provides different types of building blocks like models, views, events, routers and collections for assembling client side web applications.

Is Backbone JS open source?

Backbone is an open-source component of DocumentCloud.


1 Answers

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.

like image 158
Derick Bailey Avatar answered Sep 21 '22 18:09

Derick Bailey