Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created January 15, 2014 05:14
Show Gist options
  • Save prabirshrestha/8431197 to your computer and use it in GitHub Desktop.
Save prabirshrestha/8431197 to your computer and use it in GitHub Desktop.
simple two way bindings for reactjs using mixins
/** @jsx React.DOM */
// http://jsfiddle.net/prabir/x2VX7/5/
var ReactBindTo = {
bindTo: function (key, defaultValue) {
var self = this;
return {
value: this.state[key] || defaultValue,
requestChange: function (value) {
var state = {};
state[key] = value || defaultValue;
this.setState(state);
}.bind(this)
};
}
};
var App = React.createClass({
mixins: [ReactBindTo],
getInitialState: function () {
return { name: 'Prabir Shrestha', url: 'http://prabir.me' };
},
handleSubmit: function () {
console.log(this.state);
},
render: function () {
return (
<div>
<input type="text" valueLink={this.bindTo('name')}/>
<input type="text" valueLink={this.bindTo('url', 'test')}/>
<input type="submit" onClick={this.handleSubmit}/>
</div>
);
}
});
React.renderComponent(<App/>, document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment