-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (33 loc) · 1007 Bytes
/
index.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
'use strict';
var EventEmitter = require('events').EventEmitter;
var xtend = require('xtend');
/**
* A helper function that extends a basic Flux store with a .listenTo
* mixin and an addEventListener method that always uses the 'change' event.
* @param {Object} store Flux store
* @returns {Object} augmented store
*/
module.exports = function(opts) {
var CHANGE_EVENT = 'change';
var events = {
emitChange: function(actionType, d) {
this.emit(CHANGE_EVENT, actionType, d);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
},
listenTo: {
componentWillMount: function() {
store.addChangeListener(this._onChange);
},
componentWillUnmount: function() {
store.removeChangeListener(this._onChange);
}
}
};
var store = xtend(EventEmitter.prototype, xtend(opts, events));
return store;
};