-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathBaseStore.js
78 lines (69 loc) · 1.94 KB
/
BaseStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Copyright 2014, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
var inherits = require('inherits');
var EventEmitter = require('eventemitter3');
var CHANGE_EVENT = 'change';
/**
* @class BaseStore
* @extends EventEmitter
* @param dispatcher The dispatcher interface
* @constructor
*/
function BaseStore(dispatcher) {
this.dispatcher = dispatcher;
this._hasChanged = false;
EventEmitter.call(this);
if (this.initialize) {
this.initialize();
}
}
inherits(BaseStore, EventEmitter);
/**
* Convenience method for getting the store context object.
* @method getContext
* @return {Object} Returns the store context object.
*/
BaseStore.prototype.getContext = function getContext() {
return this.dispatcher.getContext();
};
/**
* Add a listener for the change event
* @method addChangeListener
* @param {Function} callback
*/
BaseStore.prototype.addChangeListener = function addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
};
/**
* Remove a listener for the change event
* @method removeChangeListener
* @param {Function} callback
*/
BaseStore.prototype.removeChangeListener = function removeChangeListener(
callback,
) {
this.removeListener(CHANGE_EVENT, callback);
};
/**
* Determines whether the store should dehydrate or not. By default, only dehydrates
* if the store has emitted an update event. If no update has been emitted, it is assumed
* that the store is in its default state and therefore does not need to dehydrate.
* @method shouldDehydrate
* @returns {boolean}
*/
BaseStore.prototype.shouldDehydrate = function shouldDehydrate() {
return this._hasChanged;
};
/**
* Emit a change event
* @method emitChange
* @param {*} param=this
*/
BaseStore.prototype.emitChange = function emitChange(param) {
this._hasChanged = true;
this.emit(CHANGE_EVENT, param || this);
};
module.exports = BaseStore;