Marionette itemViewEvents; event dispatching and binding
Asked Answered
H

1

5

I'm having some trouble understanding how event dispatching and binding to events between children parents work in the awesomeness that is Marionette.

Is it correct that I can trigger a custom event from an itemView like this:

var Item = Marionette.ItemView.extend({
    events: {
      "click .foo": "do:something"
    }
});

var itemCollection = Marionette.CollectionView.extend({
    itemView: item,
    initialize: function () {
        this.on("itemview:do:something", this.onSomething, this);
    }
}};

Is there some shortcut to binding to the itemView events like I would DOM events:

 var itemCollection = Marionette.CollectionView.extend({
    itemView: item,
    itemviewevents: {
        "itemview:do:something": "onSomething"
    }
}};

Thanks :).

Heartily answered 18/11, 2013 at 11:54 Comment(0)
L
8

You're confusing triggers and events. Your code should be

var Item = Marionette.ItemView.extend({
    triggers: {
      "click .foo": "do:something"
    }
});

Use the events hash to have a function called when an event takes place, use the triggers hash to have a trigger executed.

Longlived answered 18/11, 2013 at 12:29 Comment(4)
But is there any reason it would be a bad idea if it was possible to listen for the events with a configuration hash? ie. itemviewEvents: {...} ?Heartily
You should update your initial post to remove your "update", as it may confuse users unfamiliar with Marionette and triggers if they don't scroll down to the answer.Laird
Are you by any chance referring to the OP's question? If not, I'm not sure I understand how you think I should change my answer.Longlived
Remove the update thing. by request of Tyler Harden.Heartily

© 2022 - 2024 — McMap. All rights reserved.