Created
April 6, 2014 23:03
-
-
Save searler/10012337 to your computer and use it in GitHub Desktop.
react.js textarea that updates via a web service
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
var Editor = React.createClass({ | |
getInitialState: function() { | |
return {value: 'Hello!'}; | |
}, | |
handleChange: function(event) { | |
var caretPos = this.getDOMNode().selectionStart; | |
var msg = {text:event.target.value,caret:caretPos}; | |
$.ajax({url: "/text", type: "POST", | |
data: JSON.stringify(msg), | |
contentType:"application/json; charset=utf-8", dataType:"json", | |
success : function(response){ | |
this.setState({value: response.text}); | |
this.getDOMNode().setSelectionRange(response.caret,response.caret); | |
}.bind(this)}); | |
}, | |
render: function() { | |
var value = this.state.value; | |
return <textarea type="text" value={value} | |
onChange={this.handleChange} | |
rows="10" cols="60"/>; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment