Created
May 13, 2015 23:57
-
-
Save ryanflorence/35cb6be612aed04ef54f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var React = require("react"); | |
var { func } = React.PropTypes; | |
var assign = require("object-assign"); | |
var ReactComponent = Ember.Component.extend({ | |
reactProps: null, | |
reactActions: null, | |
setupReactPropsAndActions: function() { | |
var propTypes = this.get("reactComponentClass.propTypes"); | |
var props = []; | |
var actions = {}; | |
Object.keys(propTypes).forEach((name) => { | |
if (propTypes[name] === func || propTypes[name] === func.isRequired) { | |
actions[name] = function(...args) { | |
return this.sendAction.apply(this, [name].concat(args)); | |
}.bind(this); | |
} else { | |
props.push(name); | |
this.addObserver(name, () => Ember.run.throttle(this, "reactRender", 10)); | |
} | |
}); | |
this.setProperties({ | |
reactActions: actions, | |
reactProps: props, | |
}); | |
}.on("init"), | |
reactRender() { | |
var props = this.getProperties(this.get("reactProps")); | |
var actions = this.get("reactActions"); | |
var component = React.createElement( | |
this.get("reactComponentClass"), | |
assign({}, props, actions) | |
); | |
var el = this.get("element"); | |
React.render(component, el); | |
}, | |
didInsertElement () { | |
this.reactRender(); | |
}, | |
willDestroyElement () { | |
React.unmountComponentAtNode(this.get("element")); | |
} | |
}); | |
ReactComponent.reopenClass({ | |
from: function(component) { | |
return this.extend({ | |
reactComponentClass: component, | |
}); | |
}, | |
}); | |
module.exports = ReactComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This lets you:
This is two years old for both react and ember, YMMV. I'm pretty sure the team had to tweak for edge cases but I don't have access to their code anymore.