Created
April 21, 2012 06:03
-
-
Save skystrife/2434581 to your computer and use it in GitHub Desktop.
Add chained attr accessors using coffeescript classes
This file contains 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
# utility that allows us to defined chained attr accessors on classes | |
Function::chain_accessor = (attrs...) -> | |
for attr in attrs | |
# add the function to the prototype. we wrap the function in a closure | |
# to ensure that "attr" keeps its value so we are accessing the right | |
# attribute when we call this function later | |
@::[attr] = do (attr) -> | |
(data) -> | |
return @["_#{attr}"] unless data? | |
@["_#{attr}"] = data | |
return @ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment