Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter to call child view method directly from parent, or by initiating a trigger?
    primarykey
    data
    text
    <p>I've got a complex Backbone/Marionette app and I'm often venting things out to parent elements . When I need to trigger things in children from parent elements though, it's a little unclear to me how I should be doing this. The easiest thing to do is just to call the method on the child from the parent, but, I wonder if I am "cheating" in this case (and perhaps causing memory leak issues or something?) and I'm curious about the "right" way.</p> <p><a href="http://jsfiddle.net/cmcculloh/aNmM6/" rel="nofollow">Here's a jsfiddle of an extremely simplified contrived version of my code.</a> as well as <a href="https://gist.github.com/cmcculloh/6270990" rel="nofollow">a gist of the same contrived example in a require based app</a>.</p> <pre><code>&lt;header&gt; &lt;h4&gt;A subview/view trigger event loop playground&lt;/h4&gt; &lt;p&gt;Open your console and then click the button&lt;/p&gt; &lt;/header&gt; &lt;article id="main"&gt; &lt;/article&gt; &lt;script type="text/html" id="MySubView"&gt; &lt;button type="button" id="button"&gt;Click Me&lt;/button&gt; &lt;/script&gt; &lt;script type="text/html" id="MyView"&gt; &lt;div id="myRegion"&gt;&lt;/div&gt; &lt;/script&gt; &lt;script&gt; var app = new Marionette.Application(); app.addRegions({ "mainRegion": "#main" }); app.module("SampleModule", function(Mod, App, Backbone, Marionette, $, _){ var MySubView = Marionette.ItemView.extend({ template: '#MySubView', events: { 'click #button': 'sendClick' }, triggers: { 'afterClick': 'afterClick' }, initialize: function initialize(){ this.on('afterClick', this.afterClick); }, sendClick: function(e) { e.preventDefault(); console.log('Good... Use your ventful capabilities boy...'); //trigger some method in the parent... this.trigger('processClick'); }, afterClick: function(who) { console.log("I've been waiting for you... The circle is now complete. When I left you I was the but the sender, now I am the receiver...", who); } }); var MyView = Marionette.Layout.extend({ template: '#MyView', views: {}, regions: { myRegion: '#myRegion' }, onShow: function onShow(){ this.views.mySubView = new MySubView({ model: this.model }).on('processClick', this.sendBackClick, this); this.myRegion.show(this.views.mySubView); }, sendBackClick: function(){ //do some stuff, then, let the child know you're done... console.log('heh heh heh heh.... Goood, goooood! Your triggers have made you powerful. Now, fulfil your parents destiny and take the event at my side!!!'); //use trigger? this.views.mySubView.trigger('afterClick', 'from trigger'); //call method directly? this.views.mySubView.afterClick('called directly'); } }); var Controller = Marionette.Controller.extend({ initialize: function(options){ this.region = options.region }, show: function(){ var mainView = new MyView({ model: new Backbone.Model({'prop':'value'}) }); this.region.show(mainView); } }); Mod.addInitializer(function(){ Mod.controller = new Controller({ region: app.mainRegion }); Mod.controller.show(); }); }); app.start(); &lt;/script&gt; </code></pre> <p>As an aside: notice I'm using a 'view' property on the parent to keep track of it's children. If there's a better way to do this, or a reason as to why that's wrong I'd love to hear it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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