Skip to content

Instantly share code, notes, and snippets.

@valff
Created June 14, 2015 16:42
Show Gist options
  • Save valff/c6f82fe817dbf6f856e8 to your computer and use it in GitHub Desktop.
Save valff/c6f82fe817dbf6f856e8 to your computer and use it in GitHub Desktop.
React.Component Mixin
createMergedResultFunction = (f, g) ->
->
[a, b] = [f.apply(this, arguments), g.apply(this, arguments)]
return b unless a?
return a unless b?
Object.assign({}, a, b)
createChainedFunction = (f, g) ->
->
f.apply(this, arguments)
g.apply(this, arguments)
return
@Mixin = (mixin) ->
for own key, value of mixin
switch key
when 'mixins'
@mixin object for object in value
when 'statics'
@[name] = property for own name, property of value
when 'displayName'
@[key] = value
when 'childContextTypes', 'contextTypes', 'propTypes'
@[key] = Object.assign({}, @[key], value)
when 'getDefaultProps'
if @[key]
@[key] = createMergedResultFunction(@[key], value)
else
@[key] = value
else
if @::[key]
switch key
when 'getInitialState', 'getChildContext'
@::[key] = createMergedResultFunction(@::[key], value)
when 'componentWillMount', 'componentDidMount', \
'componentWillUpdate', 'componentDidUpdate', \
'componentWillUnmount'
@::[key] = createChainedFunction(@::[key], value)
else
@::[key] = value
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment