Nested Marionette Behaviors

Today I was having trouble with Nested Behaviors in Marionette 2.4.2.

The trouble was, how do I get options into my behaviors in behaviors?

Here is my situation:

view.js:

  View.Main = Marionette.ItemView.extend({
  ...
  behaviors: {
    // named so the sub behavior can access this!
    behaviorName: {
      data: 'someData',
      behaviorClass: BehaviorName,
    }
  }
  ...
});

Then inside of that Behavior:

BehaviorName.js:


...
// Note: using the unnamed array method to describe behaviors here, which came in as of 2.4.1
behaviors: [{
behaviorClass: NestedBehaviorName,
}],
...

So the problem then, is how do I get these the options from the parent into this sub behavior?

Here is what I ended up with:

NestedBehaviorName.js

  ... 
  initialize: function() {
    if (this.view.behaviors.BehaviorName) {
      _.extend(this.options, _.pick(this.view.behaviors.BehaviorName, ['data']));
    }
  },
  ...

Leave a Reply

Your email address will not be published. Required fields are marked *