Skip to content

Instantly share code, notes, and snippets.

@Verikon
Created April 6, 2015 20:28
Show Gist options
  • Save Verikon/c282f2b927944a33e10b to your computer and use it in GitHub Desktop.
Save Verikon/c282f2b927944a33e10b to your computer and use it in GitHub Desktop.
mixin class for React 0.13 ,
var React = require( 'react' );
class MixableComponent extends React.Component{
constructor( props ){
//when we can do arrow functions - remove this.
this.mix = this.mix.bind( this );
if( props.mixins )
this.mix( props.mixins );
super( props )
}
mix( mixins ){
if( !( mixins instanceof Array ) )
mixins = [ mixins ];
var mixinCount = mixins.length;
var mixinI, mixin, method, mixinname;
this.mixin = this.mixin || {};
for( mixinI in mixins ){
mixin = mixins[ mixinI ];
mixinname = mixin.mixin_name;
for( method in mixin ) {
//worry about variable support later.
if( typeof mixin[ method ] !== 'function' ){
continue;
}
//set up the namespace if we need it.
if( !this.mixin[ mixinname ] ){
this.mixin[ mixinname ] = { super: {} };
}
//copy the method to the component namespace.
this.mixin[ mixinname ][ method ] = mixin[ method ].bind( this );
if( !this[ method ] ){
this.mixin[ mixinname ].super[ method ] = function(){}.bind( this );
} else {
this.mixin[ mixinname ].super[ method ] = this[ method ].bind( this );
}
this[ method ] = mixin[ method ];
}
}
}
}
module.exports = MixableComponent;
________________
require( 'MixibleComponent.jsx' );
class myReactComponent extends MixableComponent {
constructor( props ){
super( props );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment