Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function SimpleObjectStore(storeName){
this.storeName = storeName;
this.changedEvent = "asq:" + this.storeName + "-changed";
}
SimpleObjectStore.prototype = {
constructor: SimpleObjectStore,
count: function(){
return this.get().length;
},
get: function(){
return store.get(this.storeName) || []
},
add: function(object){
var objects = this.get();
if(this.contains(object)){
return;
}
objects.push(object);
this.set(objects);
this.triggerChanged();
},
remove: function(object){
var objects = this.get();
var _this = this;
objects = _.reject(objects, function(o){
return _this.objectEquality(o, object);
});
this.set(objects);
this.triggerChanged();
},
contains: function(object){
var _this = this;
_.map(this.get(), function(o){
if(_this.objectEquality(o, object)){
return true;
}
});
return false;
},
triggerChanged: function(){
$('body').trigger(this.changedEvent);
},
set: function(objects){
store.set(this.storeName, objects);
},
clear: function(){
this.set([]);
this.triggerChanged();
},
objectEquality: function(a, b){
return a == b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment