Skip to content

Instantly share code, notes, and snippets.

@wnstn
Last active December 11, 2015 02:28
Show Gist options
  • Select an option

  • Save wnstn/4530325 to your computer and use it in GitHub Desktop.

Select an option

Save wnstn/4530325 to your computer and use it in GitHub Desktop.
PubSub method using coffeescript classes and jquery's callbacks
class @Feed
constructor: (@func)->
@callbacks = $.Callbacks()
init: ->
@subscribe( @func )
subscribe: =>
@callbacks.add( @func )
publish: (args)=>
if args.length < 1
@callbacks.fire @func
else
@callbacks.fire @func.apply @, args
init: ->
#instantiate a new feed and pass the function to subscribe
@nameOfFeed = new Feed( @someFunction)
someFunction: (arg1)=>
console.log "the argument is " + arg1
# output: "the argument is successful"
otherFunction: =>
#doSomething here, on success
@nameOfFeed.publish [ 'successful' ]
@wnstn
Copy link
Author

wnstn commented Jan 15, 2013

Updated tonight to include the ability to pass in an array of arguments with the .publish() function that will be applied to the subscribed function on execution.

This still lacks the ability to subscribe multiple functions, or to remove subscribed functions.

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