Permalink
Browse files

Add polling for data updates

  • Loading branch information...
andrewdotn committed Aug 21, 2016
1 parent bec0411 commit 243f7d4e5f7bde54fdeb147198cf21ed105560c7
Showing with 27 additions and 1 deletion.
  1. +27 −1 polls_react/polls/templates/polls/index.html
@@ -44,12 +44,38 @@ <h1>New style</h1>
});
var PollBox = React.createClass({
getInitialState: function () {
return {
data: this.props.data,
updateTime: "never"
}
},
loadPollsFromServer: function() {
$.ajax({
url: "{% url 'polls:question-list' %}",
dataType: 'json',
cache: false,
success: function (data) {
this.setState({data: data, updateTime: new Date()})
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
componentDidMount: function() {
this.loadPollsFromServer();
setInterval(this.loadPollsFromServer, 1000);
},
render: function() {
var questionNodes = this.props.data.map(function(question) {
var questionNodes = this.state.data.map(function(question) {
return (<Question data={question} key={question.url}/>);
});
return (
<div>
<ul>{ questionNodes }</ul>
Last live-update at { this.state.updateTime.toString() }
</div>
);
}
});

0 comments on commit 243f7d4

Please sign in to comment.