Skip to content

Instantly share code, notes, and snippets.

@thurloat
Created January 22, 2012 02:17
Show Gist options
  • Save thurloat/1655106 to your computer and use it in GitHub Desktop.
Save thurloat/1655106 to your computer and use it in GitHub Desktop.
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