You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/docs/01-getting-started.md
+22-258
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,8 @@ section: "Guide"
5
5
6
6
# Getting Started
7
7
8
-
This guide will take you through the process of install NuclearJS and familiarize you with the concepts found in Nuclear to build Flux systems.
8
+
This guide will take you through the process of installing NuclearJS and familiarize you with the concepts that will allow you
9
+
to build Flux systems.
9
10
10
11
## Installation
11
12
@@ -16,34 +17,42 @@ npm install --save nuclear-js
16
17
17
18
## Overview
18
19
19
-
In this tutorial we will create a Nuclear flux system to show a list of products and add them to a shopping cart. Here's the plan:
20
+
In this tutorial we'll create a Nuclear flux system to show a list of products and add them to a shopping cart. Here's the plan:
20
21
21
22
1. Create a **Reactor**
22
23
23
-
2. Create **actions** to fetch products from a server and to add a product to the shopping cart
24
+
2. Create **Actions** to fetch products from a server and to add a product to the shopping cart
24
25
25
26
3. Create a **ProductStore** and **ShoppingCartStore**
26
27
27
-
4. Create **getters** to transform and compose our store data into a consumable format for the UI
28
+
4. Create **Getters** to transform and compose our store data into a consumable format for the UI
28
29
29
30
5. Hook everything up to React
30
31
31
32
### A few things to do know before we start
32
33
33
-
1. Although the example code is written using ES6, this is totally optional. NuclearJS fully supports ES5 out of the box
34
+
1. Although the example code is written using ES6, this is totally optional. NuclearJS fully supports ES5 out of the box.
34
35
35
36
2. Nuclear stores work best when using ImmutableJS data structures. You will see `toImmutable` quite often, this is simply sugar
36
-
to convert plain javascript arrays into [`Immutable.List`](http://facebook.github.io/immutable-js/docs/#/List) and objects to
37
+
to convert plain JavaScript arrays into [`Immutable.List`](http://facebook.github.io/immutable-js/docs/#/List) and objects to
37
38
[`Immutable.Map`](http://facebook.github.io/immutable-js/docs/#/Map). The use of `toImmutable` is optional, you are free to use
38
39
any ImmutableJS data structure with no penalty.
39
40
40
41
41
42
## Creating a `Reactor`
42
43
43
-
In Nuclear the `Reactor` is the brains of the system. Generally you will only have one reactor for your application, however they are instanceable
44
-
for server-side rendering.
44
+
To get started, we'll create a Nuclear `Reactor`. In Nuclear, the `Reactor` is the brains of the system and in some ways analogous
45
+
to the traditional Flux `dispatcher` (though it works differently under the hood and provides a few extra features, which we'll
46
+
cover later).
45
47
46
-
The reactor holds the application state in the form of an `Immutable.Map`, while dispatching actions transform the application state.
48
+
Generally you'll only have one reactor for your application, however they are instanceable for server-side rendering.
49
+
50
+
The reactor has two main jobs:
51
+
52
+
1. It holds the entire application state in the form of an `Immutable.Map`
53
+
2. It dispatches actions to transform the application state
54
+
55
+
Let's begin by creating a `reactor.js` file.
47
56
48
57
#### `reactor.js`
49
58
@@ -57,254 +66,9 @@ const reactor = new Reactor({
57
66
exportdefaultreactor
58
67
```
59
68
69
+
_* If you pass a `debug: true` option when instantiating a reactor, you'll get great debugging tools that print to your browser console.
70
+
This is completely optional, but very useful for keeping tracking of dispatched actions and subsequest changes in state._
60
71
61
-
## Actions
62
-
63
-
Actions are categorized as any function that calls `reactor.dispatch(actionType, payload)`.
64
-
65
-
For our example we will start by creating actions to fetch products from a server and to add a product to the users shopping cart.
66
-
67
-
#### `actionTypes.js`
68
-
69
-
```javascript
70
-
importkeyMirrorfrom'react/lib/keyMirror'
71
-
72
-
exportdefaultkeyMirror({
73
-
RECEIVE_PRODUCTS:null,
74
-
ADD_TO_CART:null,
75
-
CHECKOUT_START:null,
76
-
CHECKOUT_SUCCESS:null,
77
-
CHECKOUT_FAILED:null,
78
-
})
79
-
```
80
-
81
-
#### `actions.js`
82
-
83
-
```javascript
84
-
importshopfrom'../../common/api/shop'
85
-
importreactorfrom'./reactor'
86
-
import {
87
-
RECEIVE_PRODUCTS,
88
-
ADD_TO_CART,
89
-
CHECKOUT_START,
90
-
CHECKOUT_SUCCESS,
91
-
CHECKOUT_FAILED,
92
-
} from'./actionTypes'
93
-
94
-
exportdefault {
95
-
fetchProducts() {
96
-
shop.getProducts(products=> {
97
-
reactor.dispatch(RECEIVE_PRODUCTS, { products })
98
-
});
99
-
},
100
-
101
-
addToCart(product) {
102
-
reactor.dispatch(ADD_TO_CART, { product })
103
-
},
104
-
}
105
-
```
106
-
107
-
108
-
## Creating Stores
109
-
110
-
Stores hold no state, instead they provide a collection of functions that transform current state into new state. They provide an `initialize` hook used when
111
-
registering with a reactor to define what actions they respond to.
112
-
113
-
In Nuclear there is no need to worry about stores knowing about other stores or `store.waitsFor`. The sole responsibility of stores is to write or mutate application
114
-
state, and the responsibility of reading application state falls on Getters.
Registering the store with a reactor does two things:
191
-
192
-
1. Passes every dispatched action to the store
193
-
2. Binds a store's state to the application state by the key used for registration
194
-
195
-
#### `main.js`
196
-
197
-
```javascript
198
-
importreactorfrom'./reactor'
199
-
importProductStorefrom'./stores/ProductStore'
200
-
importCartStorefrom'./stores/CartStore'
201
-
202
-
reactor.registerStores({
203
-
'products': ProductStore,
204
-
'cart': CartStore,
205
-
})
206
-
```
207
-
208
-
## Recap
209
-
210
-
At this point we've created actions for fetching products and adding an item to the cart. We also have the `ProductStore` and `CartStore` registered on the reactor.
211
-
212
-
Let's see what our application state looks like by using the `reactor.evaluate` function:
213
-
214
-
```javascript
215
-
// providing an empty array to `evaluate` will return a snapshot of the entire app state
216
-
reactor.evaluate([])
217
-
// result
218
-
Map {
219
-
cart:Map {
220
-
itemQty:Map {}
221
-
},
222
-
products:Map {}
223
-
}
224
-
225
-
reactor.evaluate(['cart'])
226
-
// result
227
-
Map {
228
-
itemQty:Map {}
229
-
}
230
-
```
231
-
232
-
The application state is rather empty, each top level key is populated by the store's `getInitialState()` method.
233
-
234
-
Let's see what our application state looks like after we fetch some products.
0 commit comments