Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two ways I think you could accomplish this. First, pass the parent into the child view. Second, create an eventAggregator that handles event firing.</p> <p>FIRST:</p> <p>You could pass the appView into the previewDataView as an options.</p> <pre><code>preview = new previewDataView({ // your options, 'parent':this }); </code></pre> <p>In your previewDataView you would bind to the parent custom event like so:</p> <pre><code>this.parent = this.options.parent; this.parent.bind('eventName', this.onEvent, this); </code></pre> <p>SECOND: (Just learned this technique)</p> <p>You could create an eventAggregator that helps your views subscribe to and unsubscribe for events meant for each other. Here is a good answer that explains this in detail on Stack: <a href="https://stackoverflow.com/questions/7843278/fire-an-event-from-one-view-to-another-in-backbone">fire an event from one view to another in backbone</a></p> <p>In the comments, @Brian Genisio takes it a step further to include this in his code:</p> <pre><code>Backbone.View.prototype.eventAggregator = _.extend({}, Backbone.Events); </code></pre> <p>Have this run in the start of your code and every view will have access to eventAggregator which will be your central hub for events that need to be triggered and received across views. You'd use it like this:</p> <pre><code>// Parent View this.eventAggregator.trigger('someEvent'); // Child View this.eventAggregator.bind('someEvent', this.function, this); </code></pre> <p>With this, you don't have to explicitly pass reference between views that need access to each other for event triggering and listening. This method has come in QUITE handy for me. :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload