react-templates/playground/samples/weather.code

36 lines
1.2 KiB
Plaintext
Raw Normal View History

2014-12-30 10:05:27 +01:00
var <%= name %> = React.createClass({
2014-12-07 14:13:35 +01:00
mixins: [React.addons.LinkedStateMixin],
getInitialState: function() {
this.cityIds = [5391959,293397,2643743];
this.fetchWeather();
return { loading: true, cityToAdd: '', info: [] };
},
addCity: function() {
if (this.state.cityToAdd.trim() == '') {
return;
}
this.setState({ loading: true, cityToAdd: '' });
$.get('http://api.openweathermap.org/data/2.5/weather?q=' + this.state.cityToAdd, this.findCityCallback);
},
findCityCallback: function(result) {
if (result.id && !_.contains(this.cityIds, result.id)) {
this.cityIds.unshift(result.id);
this.fetchWeather();
2014-12-30 09:42:31 +01:00
} else {
2014-12-07 14:13:35 +01:00
this.setState({ loading: false });
}
},
refresh: function() {
this.setState({ loading:true });
this.fetchWeather();
},
fetchWeather: function() {
$.get('http://api.openweathermap.org/data/2.5/group?id=' + this.cityIds.join(',') + '&units=metric', this.fetchWeatherCallback);
},
fetchWeatherCallback: function(result) {
this.setState({ loading:false, info: result.list });
2014-12-30 09:42:31 +01:00
},
render: function () {
2014-12-30 10:05:27 +01:00
return <%= name %>RT.apply(this);
2014-12-07 14:13:35 +01:00
}
2014-12-30 09:42:31 +01:00
});