Skip to content

Instantly share code, notes, and snippets.

@tvon
Created August 13, 2014 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvon/0f72c5fd061143483a42 to your computer and use it in GitHub Desktop.
Save tvon/0f72c5fd061143483a42 to your computer and use it in GitHub Desktop.
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