Backbone.js - snap from one look to another?

I have a basic view of an application with a filter menu in the header. When he clicked, I want to filter the content in a separate news view. But I don’t know how to bind events (and pass class data) from clicks in one view to a function in another.

How can i do this?

+5
source share
2 answers

There are several ways to do this, but you probably want to create a model object that is shared between two views. Then on the 'click' in the field of view one, update the model object and bind the 'on change' in view two to the model object.

Basically, you can tweak both views to stay in sync with the model object, and any changes to the object will lead to changes in the view.

+16
source

Everything in Backbone is inherited from Backbone.Events, so you can fire and bind events from anywhere ( docs for Backbone.Events ):

var View1 = Backbone.View.extend();

var View2 = Backbone.View.extend({
    eventHandler: function(data) {alert(data)}
});

var v1 = new View1;
var v2 = new View2;

v1.bind('hello-world-event', v2.eventHandler)
v1.trigger('hello-world-event', 'Hello World!')

, , v2.eventHandler, 'this' v1. . docs.

+2

All Articles