Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Modular Coffeescript Views using Observer
# All of your views should inherit from the BaseView. As a result you get the
# globalEvents functionality for free. The globalEvents hash is similar to that
# of the built-in Backbone events hash. It automatically binds event names to
# instance methods.
#
# class TestView extends ModularView
#
# globalEvents:
# "PhoneRang": "answerPhone"
#
# answerPhone: ->
# # phone answering logic
#
# Also note that the correct value of `this` is passed in.
#
class ModularView extends Backbone.View
delegateEvents: (events) ->
super
@globalEvents = @globalEvents or {}
for event, handler of @globalEvents
@options.pubSub.bind event, _.bind @[handler], @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment