forked from reflux/refluxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (73 loc) · 2.16 KB
/
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
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
79
80
81
var Reflux = require('../src');
// Creating an Action
var textUpdate = Reflux.createAction();
var statusUpdate = Reflux.createAction();
// Creating a Data Store - Listening to textUpdate action
var textStore = Reflux.createStore({
init: function() {
this.listenTo(textUpdate, this.output);
},
output: function() {
var i, args = Array.prototype.slice.call(arguments, 0);
for (i = 0; i < args.length; i++) {
this.writeOut(args[i]);
}
},
writeOut: function(text) {
this.trigger(text);
}
});
// Creating a DataStore
var statusStore = Reflux.createStore({
init: function() {
this.listenTo(statusUpdate, this.output);
},
output: function(flag) {
var status = flag ? 'ONLINE' : 'OFFLINE';
this.trigger(status);
}
});
// Creating an aggregate DataStore that is listening to textStore and statusStore
var storyStore = Reflux.createStore({
init: function() {
this.listenTo(statusStore, this.statusChanged);
this.listenTo(textStore, this.textUpdated);
this.storyArr = [];
},
statusChanged: function(flag) {
if (flag === 'OFFLINE') {
this.trigger('Once upon a time the user did the following: ' + this.storyArr.join(', '));
// empty storyArr
this.storyArr.splice(0, this.storyArr.length);
}
},
textUpdated: function(text) {
this.storyArr.push(text);
}
});
// Fairly simple view component that outputs to console
function ConsoleComponent() {
textStore.listen(function(text) {
console.log('text: ', text);
});
statusStore.listen(function(status) {
console.log('status: ', status);
});
storyStore.listen(function(story) {
console.log('story: ', story);
});
}
new ConsoleComponent();
// Invoking the action with arbitrary parameters
statusUpdate(true);
textUpdate("testing", 1337, { "test": 1337 });
statusUpdate(false);
/** Will output the following:
*
* status: ONLINE
* text: testing
* text: 1337
* text: { test: 1337 }
* story: Once upon a time the user did the following: testing, 1337, [object Object]
* status: OFFLINE
*/