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
Alternatively you can also check whether a node changes
73
-
```javascript
74
-
cortex.didChange() // => true
75
-
76
-
cortex.didChange('a') // => true
77
-
cortex.didChange('b') // => false
78
-
79
-
//same as above
80
-
cortex.a.didChange() // => true
81
-
cortex.b.didChange() // => false
82
-
```
83
-
84
-
** You SHOULD NOT use `getChanges` and `didChange` to implement `shouldComponentUpdate` unless your components ONLY rely on cortex object to rerender. The reason is these changes are computed on cortex update and retained until the next update, so if your non-cortex props or state change then your nextProps, nextState would still contain the cortex changes of the previous cycle. This may incorrectly return true and results in your components updating more frequent than they should be.
85
-
86
61
Add callbacks
87
62
```javascript
88
-
cortex.on("update", myCallback);
89
-
```
90
-
91
-
Remove callback
92
-
```javascript
93
-
cortex.off("update", myCallback);
94
-
```
95
-
96
-
Remove all callbacks
97
-
```javascript
98
-
cortex.off("update");
63
+
cortex.onUpdate(myCallback);
99
64
```
100
65
101
66
### ES6 Guide
@@ -120,6 +85,25 @@ class MyComponent extends React.Component {
120
85
}
121
86
```
122
87
88
+
# Cortex 2.0 migration guide
89
+
90
+
The biggest change in v2 is immutable data. This allows us to implement `shouldComponentUpdate` as easy as
Immutability also allows us to remove `getChanges` and `didChange` methods.
99
+
100
+
BREAKING CHANGES
101
+
- `on('update', callback) is now simply onUpdate(callback)
102
+
- `off('update', callback) is removed
103
+
-`insertAt` and `removeAt` are replaced by `splice`, which behaves the same way as `Array.prototype.splice`
104
+
-`add` is replaced by `merge`
105
+
- for aesthetic reason, `remove` and `destroy` are swapped. So you would call `remove(key)` to remove a nested child and call `destroy` to remove self.
106
+
123
107
# Overview
124
108
125
109
In React's world data flows in one direction from the top down. That means if you want to make a change, change it at the source and let it propagate down the chain. But what happens when a child component needs to update the data? React's official guideline is to use callback for communication between parent and child components.
@@ -135,6 +119,9 @@ The following example has two components, Order and Item. An Order contains an a
@@ -201,6 +191,8 @@ In Item component, note that we display the quantity value with ``this.props.ite
201
191
202
192
In `increase` method, we use ``this.props.item.quantity.set(quantity + 1)`` to add 1 to the current quantity value.
203
193
194
+
Note that we implement `shouldComponentUpdate` by simply comparing the current and next props. This comparison is extremely fast since cortex returns a brand new immutable wrapper when data change.
195
+
204
196
# Cortex API
205
197
206
198
### Initialize:
@@ -218,11 +210,8 @@ new Cortex(data, function() {
218
210
`getValue()` | Returns the actual value
219
211
`val()` | Alias for `getValue`
220
212
`set(value)` | Changes the value and rewrap the subtree.
221
-
`remove()` | Self destruct method: remove self from parent if nested, set value to undefined if root level.
222
-
`on("update", callback)` | Add a callback to run on update event (only available on root object)
223
-
`off("update", callback)`| Remove a callback. If no callback is specified, all existing callbacks will be removed (only available on root object)
224
-
`getChanges()` | Returns array of changes. Each change include the change type (either 'new', 'update', or 'delete'), the path (array of keys to the changed subtree), oldValue, and newValue
225
-
`didChange(key)` | Returns boolean value whether a change was made. key is an optional argument. When key provided, it checks whether changes occur in the key subtree. When not provided, it checks whether any change was made on the current node.
213
+
`destroy()` | Self destruct method: remove self from parent if nested, set value to undefined if root level.
214
+
`onUpdate(callback)` | Add a callback to run on update event (only available on root object)
226
215
227
216
### Cortex wrapper of array data has the following methods:
228
217
@@ -238,8 +227,7 @@ new Cortex(data, function() {
238
227
`pop()` | Removes the last element in the array
239
228
`unshift(value)` | Inserts and rewrap the value at the front of the array.
240
229
`shift()` | Removes the first element in the array
241
-
`insertAt(index, [value])` | Inserts a value or an array of values starting at specified index.
242
-
`removeAt(index, howMany = 1)` | Removes specified number of elements starting at index location. By default it removes 1 element if number of elements to be removed isn't specified.
230
+
`splice(index, removeCount, element1 [, element2, ...])` | Remove `removeCount` from the array and insert elements into the array. This is similar to the native `Array.prototype.splice` method
243
231
244
232
### Cortex wrapper of hash data has the following methods:
245
233
Methods | Description
@@ -248,17 +236,8 @@ new Cortex(data, function() {
248
236
`values()` | Returns the array of values
249
237
`hasKey(key)` | Returns boolean value whether the key exists
250
238
`forEach(callback)` | Iterates over every key and value pair. The callback accepts the following inputs `(key, wrapperElement)`
251
-
`destroy(key)` | Removes the specified key and value pair
252
-
`add(key, value)` | Adds the specified key and value pair
`Cortex.deepDiff(oldValue, newValue)` | performs deep diff on 2 given objects
260
-
`Cortex.deepClone(value)` | returns a deep copy of a given value
261
-
239
+
`remove(key)` | Removes the specified key and value pair
240
+
`merge({key1: value1[, key2: value2, ...]})` | Adds/modifies the specified key and value pairs
262
241
263
242
# CDN
264
243
@@ -315,7 +294,7 @@ Besides providing the convenience of allowing you to update data from any level,
315
294
316
295
### 1. Deep comparison between old and new values
317
296
318
-
When you issue a `set(newValue)` call, no data actually changes at that point. What happens internally is the wrapper being called publishes a notification to the master cortex wrapper passing along a payload consisting of the path for locating the data and the new value (Yes, there is a pub/sub system within Cortex.) The master wrapper then performs a deep comparison between the old and new data to determine whether it should trigger the update action. If no change was made, the process just exits without touching the data nor invoking the callbacks.
297
+
When you issue a `set(newValue)` call, no data actually changes at that point. What happens internally is the wrapper being called publishes a notification to the master cortex wrapper passing along a payload consisting of the path for locating the data and the new value (Yes, there is a pub/sub system within Cortex.) The root wrapper then performs a deep comparison between the old and new data to determine whether it should trigger the update action. If no change was made, the process just terminates without touching the data nor invoking the callbacks.
319
298
320
299
Deep comparison may sound costly but in practice when you call `set(newValue)` the newValue usually isn't deeply nested (if it is and the actual change is many layers deep then you should consider calling `set(newValue)` on the wrapper at the level that the change actually occurs.) In some situations where you have to pass in arbirarily deeply nested value the comparison work is still worth it because it can potentially save you from unnecessarily rewrapping and triggering React to update.
0 commit comments