This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[resolves #16] Added utils for creating stores
- Loading branch information
Showing
6 changed files
with
151 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright 2014, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
'use strict'; | ||
|
||
var util = require('util'), | ||
EventEmitter = require('events').EventEmitter, | ||
CHANGE_EVENT = 'change'; | ||
|
||
/** | ||
* @class BaseStore | ||
* @param dispatcher The dispatcher interface | ||
* @constructor | ||
*/ | ||
function BaseStore(dispatcher) { | ||
this.dispatcher = dispatcher; | ||
if (this.initialize) { | ||
this.initialize(); | ||
} | ||
} | ||
|
||
util.inherits(BaseStore, EventEmitter); | ||
|
||
/** | ||
* Add a listener for the change event | ||
* @param {Function} callback | ||
*/ | ||
BaseStore.prototype.addChangeListener = function(callback) { | ||
this.on(CHANGE_EVENT, callback); | ||
}; | ||
|
||
/** | ||
* Remove a listener for the change event | ||
* @param {Function} callback | ||
*/ | ||
BaseStore.prototype.removeChangeListener = function(callback) { | ||
this.removeListener(CHANGE_EVENT, callback); | ||
}; | ||
|
||
/** | ||
* Emit a change event | ||
*/ | ||
BaseStore.prototype.emitChange = function() { | ||
this.emit(CHANGE_EVENT); | ||
}; | ||
|
||
module.exports = BaseStore; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2014, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
'use strict'; | ||
|
||
var util = require('util'), | ||
BaseStore = require('./BaseStore'), | ||
IGNORE_ON_PROTOTYPE = ['statics', 'storeName', 'handlers']; | ||
|
||
/** | ||
* Helper for creating a store class | ||
* @param {Object} spec | ||
* @param {String} spec.storeName The name of the store | ||
* @param {Object} spec.handlers Hash of action name to method name of action handlers | ||
* @param {Function} spec.initialize Function called during construction for setting the default state | ||
* @param {Function} spec.dehydrate Function that returns serializable data to send to the client | ||
* @param {Function} spec.rehydrate Function that takes in serializable data to rehydrate the store | ||
*/ | ||
module.exports = function createStore(spec) { | ||
spec.statics = spec.statics || {}; | ||
if (!spec.storeName && !spec.statics.storeName) { | ||
throw new Error('createStore called without a storeName'); | ||
} | ||
var Store = function (dispatcher) { | ||
BaseStore.call(this, dispatcher); | ||
}; | ||
|
||
util.inherits(Store, BaseStore); | ||
|
||
if (spec.statics) { | ||
Object.keys(spec.statics).forEach(function (prop) { | ||
Store[prop] = spec.statics[prop]; | ||
}); | ||
} | ||
Store.storeName = spec.storeName; | ||
Store.handlers = spec.handlers; | ||
|
||
Object.keys(spec).forEach(function (prop) { | ||
if (-1 !== IGNORE_ON_PROTOTYPE.indexOf(prop)) { | ||
return; | ||
} | ||
Store.prototype[prop] = spec[prop]; | ||
}); | ||
|
||
return Store; | ||
}; |