Collection.map
by Reiot
JavaScript
$(function() {
var warcloud = {};
warcloud.Collection = function() {};
warcloud.Collection = function() {};
$.extend(warcloud.Collection.prototype, {
entities: [],
add: function(e) {
this.entities.push(e);
},
remove: function(id) {
var len = this.entities.length;
for (var i = 0; i < len; i++) {
if (this.entities[i].id == id) {
return this.entities.splice(i, 1);
}
}
console.warn('entity not found', id);
return null;
},
get: function(id) {
var len = this.entities.length;
for (var i = 0; i < len; i++) {
var e = this.entities[i];
if (e.id == id) {
return e;
}
}
console.warn('entity not found', id);
return null;
},
empty: function() {
this.entities = [];
},
len: function() {
return this.entities.length;
},
map: function(f) {
var i = 0;
while (i < this.entities.length) {
if (f(this.entities[i], i) == null) {
this.entities.splice(i, 1);
} else {
i = i + 1;
}
}
}
});
/*
build timer
*/
warcloud.buildTimer = function() {
this.timer = null;
};
$.extend(warcloud.buildTimer.prototype, warcloud.Collection.prototype, {
start: function() {
if (!this.timer) {
console.log('build timer started');
var self = this;
self.timer = window.setInterval(function() {
self.update();
}, 1000);
}
},
stop: function() {
console.log('build timer stopped');
if (this.timer) {
...