Created
August 13, 2014 17:52
-
-
Save tvon/0f72c5fd061143483a42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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