Skip to content

Instantly share code, notes, and snippets.

@seadowg
Created August 13, 2012 01:13
Show Gist options
  • Save seadowg/3336140 to your computer and use it in GitHub Desktop.
Save seadowg/3336140 to your computer and use it in GitHub Desktop.
Prototype FRP + Backbone collision
class Frappuccino.Backbone.Event extends Backbone.Event
event: (event_name) -> new Frappuccino.Event(this, event_name)
class Frappuccino.Event extends Backbone.Event
constructor: (origin, event) ->
@origin = origin
@event = event
@origin.bind(@event, (value) => this.occur(value))
occur: (value) ->
this.trigger('occur', value)
hook: (func) ->
this.bind('occur', (value) -> func(value))
map: (func) ->
pusher = new Backbone.Event()
this.hook((value) -> pusher.trigger('push', func(value))
new Frappuccino.Event(pusher, 'push')
filter: (func) ->
pusher = new Backbone.Event()
this.hook((value) ->
if func(value)
pusher.trigger('push', value)
)
new Frappuccino.Event(pusher, 'push')
merge: (event) ->
pusher = new Backbone.Event()
this.hook((value) -> pusher.trigger('push', value))
event.hook((value) -> pusher.trigger('push', value))
new Frappuccino.Event(pusher, 'push')
@seadowg
Copy link
Author

seadowg commented Aug 13, 2012

This would allow you to carry out FRP-esque magic on Backbone events. For instance:

timeline = Comments.event('add').merge(Likes.event('add'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment