Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JedWatson/store-prototype
Simple class for creating event-driven Data Stores
https://github.com/JedWatson/store-prototype
Last synced: 3 months ago
JSON representation
Simple class for creating event-driven Data Stores
- Host: GitHub
- URL: https://github.com/JedWatson/store-prototype
- Owner: JedWatson
- License: mit
- Created: 2015-02-23T00:41:17.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-07-22T08:34:04.000Z (over 9 years ago)
- Last Synced: 2024-08-09T21:14:31.640Z (3 months ago)
- Language: JavaScript
- Size: 128 KB
- Stars: 32
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Store Prototype
===============Simple class for creating event-driven Data Stores.
Install from npm with `npm install --save store-prototype` and use as per the example below.
See [this gist](https://gist.github.com/JedWatson/18eb2582f5d957053a1d) for a more complete usage example.
## Example:
```
var Store = require('store-prototype');var MyStore = new Store();
var _things = {};MyStore.extend({
getThing: function(key) {
return _things[key];
},addThing: function(key, data) {
_things[key] = data;
this.notifyChange();
},removeThing: function(key) {
delete _things[key];
this.notifyChange();
}});
MyStore.addChangeListener(function() {
console.log('Things changed!');
});```
## Named changes
You can provide a key to `addChangeListener` and `notifyChange` to get a little bit more granular control over when change listeners are called.
```
MyStore.addChangeListener('key', function() {
console.log('things changed because key!');
});
MyStore.notifyChange(); // ...
MyStore.notifyChange('key'); // things changed because key!
```