Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
'use strict';
2
3
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
4
5
var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
6
7
var _Flummox$Store$Actions = require('../Flux');
8
9
describe('Examples:', function () {
10
11
/**
12
* A simple Flummox example
13
*/
14
describe('Messages', function () {
15
16
/**
17
* To create some actions, create a new class that extends from the base
18
* Actions class. Methods on the class's prototype will be converted into
19
* actions, each with its own action id.
20
*
21
* In this example, calling `newMessage` will fire the dispatcher, with
22
* a payload containing the passed message content. Easy!
23
*/
24
25
var MessageActions = (function (_Actions) {
26
function MessageActions() {
27
_classCallCheck(this, MessageActions);
28
29
if (_Actions != null) {
30
_Actions.apply(this, arguments);
31
}
32
}
33
34
_inherits(MessageActions, _Actions);
35
36
MessageActions.prototype.newMessage = function newMessage(content) {
37
38
// The return value from the action is sent to the dispatcher.
39
return content;
40
};
41
42
return MessageActions;
43
})(_Flummox$Store$Actions.Actions);
44
45
/**
46
* Now we need to a Store that will receive payloads from the dispatcher
47
* and update itself accordingly. Like before, create a new class that
48
* extends from the Store class.
49
*
50
* Stores are automatically registered with the dispatcher, but rather than
51
* using a giant `switch` statement to check for specific action types, we
52
* register handlers with action ids, or with a reference to the action
53
* itself.
54
*
55
* Stores have a React-inspired API for managing state. Use `this.setState`
56
* to update state within your handlers. Multiple calls to `this.setState`
57
* within the same handler will be batched. A change event will fire after
58
* the batched updates are applied. Your view controllers can listen
59
* for change events using the EventEmitter API.
60
*/
61
62
var MessageStore = (function (_Store) {
63
64
// Note that passing a flux instance to the constructor is not required;
65
// we do it here so we have access to any action ids we're interested in.
66
67
function MessageStore(flux) {
68
_classCallCheck(this, MessageStore);
69
70
// Don't forget to call the super constructor
71
_Store.call(this);
72
73
var messageActions = flux.getActions('messages');
74
this.register(messageActions.newMessage, this.handleNewMessage);
75
this.messageCounter = 0;
76
77
this.state = {};
78
}
79
80
_inherits(MessageStore, _Store);
81
82
MessageStore.prototype.handleNewMessage = function handleNewMessage(content) {
83
var _setState;
84
85
var id = this.messageCounter++;
86
87
this.setState((_setState = {}, _setState[id] = {
88
content: content,
89
id: id }, _setState));
90
};
91
92
return MessageStore;
93
})(_Flummox$Store$Actions.Store);
94
95
/**
96
* Here's where it all comes together. Extend from the base Flummox class
97
* to create a class that encapsulates your entire flux set-up.
98
*/
99
100
var Flux = (function (_Flummox) {
101
function Flux() {
102
_classCallCheck(this, Flux);
103
104
_Flummox.call(this);
105
106
// Create actions first so our store can reference them in
107
// its constructor
108
this.createActions('messages', MessageActions);
109
110
// Extra arguments are sent to the store's constructor. Here, we're
111
// padding a reference to this flux instance
112
this.createStore('messages', MessageStore, this);
113
}
114
115
_inherits(Flux, _Flummox);
116
117
return Flux;
118
})(_Flummox$Store$Actions.Flummox);
119
120
/**
121
* And that's it! No need for singletons or global references -- just create
122
* a new instance.
123
*
124
* Now let's test it.
125
*/
126
127
it('creates new messages', function () {
128
var _expect$to$deep$equal;
129
130
var flux = new Flux();
131
var messageStore = flux.getStore('messages');
132
var messageActions = flux.getActions('messages');
133
134
expect(messageStore.state).to.deep.equal({});
135
136
messageActions.newMessage('Hello, world!');
137
expect(messageStore.state).to.deep.equal((_expect$to$deep$equal = {}, _expect$to$deep$equal[0] = {
138
content: 'Hello, world!',
139
id: 0 }, _expect$to$deep$equal));
140
});
141
});
142
});
143