Skip to content

Instantly share code, notes, and snippets.

@pyykkis
Last active December 11, 2015 02:48
Show Gist options
  • Save pyykkis/4532713 to your computer and use it in GitHub Desktop.
Save pyykkis/4532713 to your computer and use it in GitHub Desktop.
Backbone.EventStream =
asEventStream: (eventName, eventTransformer = _.identity) ->
eventTarget = this
new Bacon.EventStream (sink) ->
handler = (args...) ->
reply = sink(new Bacon.Next(eventTransformer args...))
if reply == Bacon.noMore
unbind()
unbind = -> eventTarget.off(eventName, handler)
eventTarget.on(eventName, handler, this)
unbind
_.extend Backbone.Model.prototype, Backbone.EventStream
_.extend Backbone.Collection.prototype, Backbone.EventStream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment