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: README.md
+3-368Lines changed: 3 additions & 368 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,375 +42,10 @@ NuclearJS can be downloaded from npm.
42
42
npm install nuclear-js
43
43
```
44
44
45
-
## Let's see some examples
46
-
47
-
Let's see what the original [Flux Chat Example](https://github.com/facebook/flux/tree/master/examples/flux-chat) looks like in NuclearJS.
48
-
49
-
All of the above code lives in [examples/flux-chat](./examples/flux-chat)
50
-
51
-
##### `flux.js`
52
-
53
-
```js
54
-
// create the Nuclear reactor instance, this will act as our dispatcher and interface for data fetching
55
-
var Nuclear =require('nuclear-js')
56
-
57
-
module.exports=newNuclear.Reactor({
58
-
debug:true,
59
-
})
60
-
```
61
-
62
-
### Modules
63
-
64
-
The prescribed way of code organization in NuclearJS is to group all stores, actions and getters of the same domain in a module.
65
-
66
-
##### Example Module File Structure
67
-
68
-
For the flux-chat example we will create a chat module that holds all of the domain logic for the chat aspect. For smaller projects there may only need to be one module, but for larger projects using many modules can decouple your codebase and make it much easier to manage.
69
-
70
-
```js
71
-
modules/chat
72
-
├── stores/
73
-
└── thread-store.js
74
-
└── current-thread-id-store.js
75
-
├── actions.js// exports functions that call flux.dispatch
76
-
├── action-types.js// constants for the flux action types
77
-
├── getters.js// getters exposed by the module providing read access to module's stores
78
-
├── index.js// MAIN ENTRY POINT - facade that exposes a public api for the module
79
-
└── tests.js// module unit tests that test the modules stores, getters, and actions
- Modules expose a single public API, the `index.js` file. It is improper for an outside piece of code to require any file within the module except the `index.js` file.
100
-
101
-
- Stores are registered lazily through the module's index.js. This may seem weird at first, but in NuclearJS stores are more of an implementation detail and not ever directly referenceable.
102
-
103
-
- Data access to the module's store values is done entirely through the getters it exposes. This provides a decoupling between the store implementation and how the outside world references the state that a module manages. A getter is a contract between the outside world and the module that a particular piece of information is accessible. The evaluator of a getter does not care about the underlying store representation.
104
-
105
-
### Stores
106
-
107
-
##### `modules/chat/stores/thread-store.js`
108
-
109
-
```js
110
-
var Nuclear =require('nuclear-js')
111
-
var toImmutable =Nuclear.toImmutable
112
-
var actionTypes =require('../action-types')
113
-
114
-
module.exports=newNuclear.Store({
115
-
getInitialState() {
116
-
// for Nuclear to be so efficient all state must be immutable data
117
-
// mapping of threadID => Thread
118
-
returntoImmutable({})
119
-
},
120
-
121
-
initialize() {
122
-
// all action handlers are pure functions that take the current state and payload
At this point defined how our application manages state over time by creating and registering the thread store and currentThreadID store. When defining stores there is no need to worry about computable state like the most recent message in each thread, this is all handled through getters.
208
-
209
-
### Getters
210
-
211
-
Getters can take 2 forms:
212
-
213
-
1. A KeyPath such as `['messages']` which equates to a `state.getIn(['messages'])` on the app state `Immutable.Map`.
214
-
2. An array with the form `[ [keypath | getter], [keypath | getter], ..., tranformFunction]`
215
-
216
-
##### `modules/chat/getters.js`
217
-
218
-
```js
219
-
// it is idiomatic to facade all data access through getters, that way a component only has to subscribe to a getter making it agnostic
220
-
// to the underlying stores / data transformation that is taking place
if (!thread.get('messages').last().get('isRead')) {
256
-
accum++
257
-
}
258
-
return accum
259
-
}, 0)
260
-
}
261
-
]
262
-
```
263
-
264
-
Since stores are registered on the Nuclear Reactor by the module's index file, then a module is the only part of the system that knows the store ids, if this information need to be made public, the module will export a getter of the form `[<storeId>]`
265
-
266
-
267
-
### Actions
268
-
269
-
##### `module/chat/actions.js`
270
-
271
-
```js
272
-
var flux =require('../../flux')
273
-
var actionTypes =require('./action-types')
274
-
var getters =require('./getters')
275
-
276
-
/**
277
-
* Handles the receiving of messages into the flux system
`flux.ReactMixin` handles all of the pub/sub between the flux system and component and will only render the component via a `setState` call whenever any of the subscribed getters' value changes. The mixin will also automatically unsubscribe from observation when the component is unmounted.
363
-
364
-
##### `ThreadListItem.react.js`
365
-
366
-
```js
367
-
var React =require('react');
368
-
var Chat =require('../modules/chat');
369
-
var cx =require('react/lib/cx');
370
-
371
-
var ReactPropTypes =React.PropTypes;
372
-
373
-
var ThreadListItem =React.createClass({
374
-
375
-
propTypes: {
376
-
thread:ReactPropTypes.object,
377
-
currentThreadID:ReactPropTypes.string
378
-
},
379
-
380
-
render:function() {
381
-
var thread =this.props.thread;
382
-
var lastMessage =thread.get('messages').last();
383
-
var dateString = (newDate(lastMessage.get('timestamp'))).toLocaleTimeString()
0 commit comments