Last active
August 29, 2015 14:15
-
-
Save ovcharik/9d5d926781d8cb379d3d to your computer and use it in GitHub Desktop.
Coffee tricks
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
@EventMixin = | |
_eventHandlers: -> | |
@__eventHandlers ||= {} | |
_getHandlers: (name) -> | |
@_eventHandlers()[name] ||= [] | |
return @_eventHandlers()[name] | |
_setHandlers: (name, value) -> | |
@_eventHandlers()[name] ||= value | |
return | |
on: (name, callback) -> | |
return unless callback | |
@_getHandlers(name).push callback | |
off: (name, callback) -> | |
unless callback | |
@_setHandlers(name, []) | |
else | |
@_setHandlers name, @_getHandlers(name).filter (c) -> | |
c == callback | |
return | |
trigger: (name, args...) -> | |
for cb in @_getHandlers(name) | |
return if cb.apply(@, args) == false | |
return |
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
runCallbacks = (type, func, args) -> | |
cbs = @[type]?[func] | |
return unless cbs | |
for cb in cbs | |
cb.apply(@, args) | |
module.exports = | |
included: -> | |
cbs = _.uniq _.keys(@::before).concat(_.keys(@::after)) | |
for f in cbs when _.isFunction(@::[f]) | |
old = @::[f] | |
cls = @ | |
do (old, cls, f) => | |
cls::[f] = (args...) -> | |
runCallbacks.call @, 'before', f, args | |
r = old.apply(@, args) | |
runCallbacks.call @, 'after' , f, args | |
return r |
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
moduleKeywords = ['extended', 'included'] | |
class @Module | |
@extend: (obj) -> | |
for key, value of obj when key not in moduleKeywords | |
@[key] = value | |
obj.extended?.apply(@) | |
this | |
@include: (obj) -> | |
for key, value of obj when key not in moduleKeywords | |
# Assign properties to the prototype | |
@::[key] = value | |
obj.included?.apply(@) | |
this |
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
@PropertyMixin = | |
property: (prop, options) -> | |
Object.defineProperty @prototype, prop, options | |
addProperty: (name, cbs...) -> | |
@property name, | |
get: -> @["_#{name}"] | |
set: (value) -> | |
n = "set#{name.capitalize()}" | |
if @[n]? | |
r = @[n](value) | |
else | |
r = @setProp(name, value) | |
for cb in cbs | |
@[cb]?() | |
r | |
extended: -> | |
@::setProp = (name, value) -> | |
if @["_#{name}"] != value | |
@["_#{name}"] = value | |
@trigger? "change:#{name}", @["_#{name}"] | |
@["_#{name}"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment