Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Last active August 29, 2015 14:02
Show Gist options
  • Save willmendesneto/a2fe1e052327ba5b5dee to your computer and use it in GitHub Desktop.
Save willmendesneto/a2fe1e052327ba5b5dee to your computer and use it in GitHub Desktop.
World Cup in six lines of Javascript based in @fmasanori jogos.py. Zepto/Jquery dependency
// Example in http://codepen.io/willmendesneto/pen/Iguor
$.get('http://worldcup.sfg.io/matches', function(data){
data.forEach(function(item){
if (item.status === 'completed') {
console.log(item.home_team.country + ' ' + item.home_team.goals + ' x ' + item.away_team.country + ' ' + item.away_team.goals);
}
});
}, 'json');
(function(window, $){
'use strict';
// Example in http://codepen.io/willmendesneto/pen/jaDuC
// Using some things of functional javascript
$.get('http://worldcup.sfg.io/matches', function(data){
var completePlays = [];
data.filter(function(item, i){
if (item.status === 'completed') {
return item;
}
}, []).sort(function(a,b){
return new Date(a.datetime) - new Date(b.datetime);
}).forEach(function(item, i){
completePlays.push(item.location + ': ' + item.home_team.country + ' ' + item.home_team.goals + ' x ' + item.away_team.country + ' ' + item.away_team.goals );
});
console.log(completePlays.join(''));
});
})(window, window.Zepto || window.jQuery);
(function(window, $){
'use strict';
// Example in http://codepen.io/willmendesneto/pen/zBJae
// Using some things of Javascript OOP
function WorldCup(){
this.completePlays = [];
this.url = 'http://worldcup.sfg.io/matches';
}
WorldCup.prototype.loadData = function() {
var self = this
$.get(this.url, function(data){
data = self.filterData(data);
self.listsPlays(data);
console.log(self.completePlays.join('\n'));
});
};
WorldCup.prototype.filterByCompletePlays = function(data) {
return data.filter(function(item, i){
if (item.status === 'completed') {
return item;
}
}, []);
};
WorldCup.prototype.orderByDatetime = function(data) {
return data.sort(function(a,b){
return new Date(a.datetime) - new Date(b.datetime);
});
};
WorldCup.prototype.listsPlays = function(data) {
var self = this;
data.forEach(function(item, i){
self.completePlays.push(item.location + ': ' + item.home_team.country + ' ' + item.home_team.goals + ' x ' + item.away_team.country + ' ' + item.away_team.goals );
});
};
WorldCup.prototype.filterData = function(data) {
return this.orderByDatetime(this.filterByCompletePlays(data));
};
var WorldCup = new WorldCup();
WorldCup.loadData();
})(window, window.Zepto || window.jQuery);
@aheinze
Copy link

aheinze commented Jun 24, 2014

if Zepto doesn't existst, then use jQuery

(function(window, $){ 
...
})(window, window.Zepto || window.jQuery);

@willmendesneto
Copy link
Author

@aheinze, thanks for contribution! Updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment