Last active
January 2, 2016 10:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyView = Marionette.ItemView.extend({ | |
listeners: { | |
model: { | |
'add': 'modelAdd' | |
}, | |
collection: { | |
'reset': 'reset' | |
}, | |
appVent: { | |
'iloveevents': 'unicorns' | |
} | |
}, | |
initialize: function(options) { | |
this.appVent = options.appVent; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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