Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone JS: can one view trigger updates in other views?

In my simple project I have 2 views - a line item view (Brand) and App. I have attached function that allows selecting multiple items:

 var BrandView = Backbone.View.extend({ ...some code...     toggle_select: function() {         this.model.selected = !this.model.selected;         if(this.model.selected) $(this.el).addClass('selected');         else $(this.el).removeClass('selected');         return this;     } });  var AppView = Backbone.View.extend({ ...some code...     delete_selected: function() {         _.each(Brands.selected(), function(model){              model.delete_selected();         });         return false;     }, }); 

Thing is, I want to know how many items are selected. In this setup selecting is NOT affecting the model and thus not firing any events. And from MVC concept I understand that views should not be directly talking to other views. So how can AppView know that something is being selected in BrandViews?

And more specifically, I AppView to know how many items were selected, so if more than 1 is selected, I show a menu for multiple selection.

like image 599
mvbl fst Avatar asked Apr 02 '12 22:04

mvbl fst


People also ask

How do I trigger an event in Backbone JS?

js trigger Event is used to invoke or callback the function for the given event or a space-delimited list of events. The subsequent arguments will be passed along to the event callbacks in order to trigger it. Parameter Values: event: It is used to bind an object with an event.

How does Backbone js work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

Why use Backbone js?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.

Which method can be used to manipulate the backbone JS history?

There is only method named "start" can be used to manipulate the Backbone. js history.


2 Answers

You might want to have a read of this discussion of Backbone pub/sub events:

http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

I like to add it in as a global event mechanism:

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

Then in one view you can trigger an event:

Backbone.pubSub.trigger('my-event', payload); 

And in another you can listen:

Backbone.pubSub.on('my-event', this.onMyEvent, this); 
like image 146
stusmith Avatar answered Sep 20 '22 15:09

stusmith


I use what Addy Osmani calls the mediator pattern http://addyosmani.com/largescalejavascript/#mediatorpattern. The whole article is well worth a read.

Basically it is an event manager that allows you to subscribe to and publish events. So your AppView would subscript to an event, i.e. 'selected'. Then the BrandView would publish the 'selected' event.

The reason I like this is it allows you to send events between views, without the views being directly bound together.

For Example

var mediator = new Mediator(); //LOOK AT THE LINK FOR IMPLEMENTATION  var BrandView = Backbone.View.extend({     toggle_select: function() {         ...         mediator.publish('selected', any, data, you, want);         return this;     } });  var AppView = Backbone.View.extend({     initialize: function() {         mediator.subscribe('selected', this.delete_selected)     },      delete_selected: function(any, data, you, want) {         ... do something ...     }, }); 

This way your app view doesn't care if it is a BrandView or FooView that publishes the 'selected' event, only that the event occured. As a result, I find it a maintainable way to manage events between parts of you application, not just views.

If you read further about the 'Facade', you can create a nice permissions structure. This would allow you to say only an 'AppView' can subscribe to my 'selected' event. I find this helpful as it makes it very clear where the events are being used.

like image 42
John McKim Avatar answered Sep 17 '22 15:09

John McKim