Skip to content

Instantly share code, notes, and snippets.

@cobbweb
Last active January 2, 2016 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobbweb/8292043 to your computer and use it in GitHub Desktop.
Save cobbweb/8292043 to your computer and use it in GitHub Desktop.
Idea for listening to objects in a view, runs after `initialize` so you can add custom vent objects too.
var MyView = Marionette.ItemView.extend({
listeners: {
model: {
'add': 'modelAdd'
},
collection: {
'reset': 'reset'
},
appVent: {
'iloveevents': 'unicorns'
}
},
initialize: function(options) {
this.appVent = options.appVent;
}
});
_.extend(Marionette.ItemView.prototype, {
_bindListeners: function() {
if (!this.listeners) { return; }
_.each(this.listeners, function(events, objectKey) {
if (!this[objectKey]) {
throw 'The view doesn\'t have an object named this.' + objectKey;
}
_.each(events, function(method, eventName) {
if (!this[method]) {
throw 'The view doesn\'t have a this.' + method + ' method';
}
this.listenTo(this[objectKey], eventName, this[method]);
}, this);
}, this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment