Last active
December 11, 2015 02:28
-
-
Save wnstn/4530325 to your computer and use it in GitHub Desktop.
PubSub method using coffeescript classes and jquery's callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' ] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.