Last active
August 29, 2015 14:21
-
-
Save tommymarshall/16da15ea29dfc44aaac4 to your computer and use it in GitHub Desktop.
simple ajax fill options
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 request = require('superagent') | |
module.exports = React.createClass({ | |
getInitialState: function() { | |
return { | |
ready : false, | |
options : [], | |
} | |
}, | |
componentDidMount: function() { | |
this.getOptions() | |
}, | |
getOptions: function() { | |
request | |
.get('/route') | |
.set('Accept', 'application/json') | |
.end(function(error, res){ | |
if (error) return false | |
this.setState({ | |
ready : true, | |
options : JSON.parse(res.text) | |
}) | |
}.bind(this)) | |
}, | |
renderOptions: function(option) { | |
return <option>{ option }</option> | |
} | |
render: function() { | |
if (!this.state.ready) { | |
return ( | |
<div> | |
Loading... | |
</div> | |
) | |
} | |
return ( | |
<select> | |
{this.options.map(this.renderOptions)} | |
</select> | |
) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment