var Events = { | |
// Store each registered $.Callbacks object by namespace | |
cache : {}, | |
// Bind callback to event along with a context | |
bind : function( ev, callback, context, options ) { | |
// Make $.Callbacks default to having stopOnFalse | |
options = options || 'stopOnFalse'; | |
// If this callback list does not exist, create it | |
if ( !this.cache[ ev ] ) { | |
this.cache[ ev ] = $.Callbacks( options ); | |
} | |
// Add callback to cache, make sure callback gets proper context | |
this.cache[ ev ].add( $.proxy( callback, context ) ); | |
}, // bind | |
unbind : function( ev, callback ) { | |
// Ignore unbind if it didn't exist | |
if ( !this.cache[ ev ] ) { | |
return; | |
} | |
this.cache.remove( callback ); | |
}, // unbind | |
trigger : function( ev ) { | |
// Ignore trigger if it doesn't exist | |
if ( !this.cache[ ev ] ) { | |
return; | |
} | |
// Get dynamic number of arguments, knowing first argument is always event | |
var args = [].slice.call( arguments ), | |
pass = args.splice(1); | |
// Call $.Callbacks fire method with right arguments | |
this.cache[ ev ].fire.apply( null, pass ); | |
} // trigger | |
}; // Events |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
jaubourg
commented
Nov 22, 2011
Line #30: you missed the [ev] |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
Thanks @jaubourg for that catch on #30. I didn't need unbind where I first wrote this so didn't see that bug yet. Also you're right about fireWith since fire just calls fireWith anyway. Didn't look at the internals until just now. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
jaubourg
Nov 23, 2011
Here is my take on it. I have removed the options in bind given how situational it is (will work the first time, not the second, etc).
jaubourg
commented
Nov 23, 2011
Here is my take on it. I have removed the options in bind given how situational it is (will work the first time, not the second, etc). |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
DaveStein
Nov 23, 2011
That's a great point about the options. I'm gonna see if I can re-configure
it so options can be changed after the fact.
Changing it from a singleton to a revealing module is something I should
probably do, to avoid cache from being exposed.
Thanks for all the input. I'll give a shout on the post and on twitter once
I finish making the changes.
…On Tue, Nov 22, 2011 at 7:57 PM, Julian Aubourg < ***@***.*** > wrote:
Here is my take on it. I have removed the options in bind given how
situational it is (will work the first time, not the second, etc).
https://gist.github.com/1387553
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1384615
That's a great point about the options. I'm gonna see if I can re-configure
it so options can be changed after the fact.
Changing it from a singleton to a revealing module is something I should
probably do, to avoid cache from being exposed.
Thanks for all the input. I'll give a shout on the post and on twitter once
I finish making the changes.
…On Tue, Nov 22, 2011 at 7:57 PM, Julian Aubourg < ***@***.*** > wrote:
Here is my take on it. I have removed the options in bind given how
situational it is (will work the first time, not the second, etc).
https://gist.github.com/1387553
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1384615
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line #30: you missed the [ev]
Line #46: you could use fireWith( null, pass );