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
This is React add-on designed to replace Backbone views with React in existing Backbone application.
1
+
# nestedreact
2
+
This is React add-on designed to simplify migration to React views in large Backbone applications.
3
3
4
4
It allows you:
5
5
6
-
- To use React component in place of every Backbone View in your application.
7
-
- To use your existing Backbone Views as subviews in React components.
6
+
- To use React component in place of every Backbone View.
7
+
- To use your existing Backbone Views from React components.
8
8
- To use your existing Backbone Models as React component state.
9
9
- Update React components on Backbone events.
10
+
- Data-binding for models and collections
10
11
11
12
Thus, no refactoring of your application is required. You can start writing UI with React immediately replacing your Backbone Views one-by-one, while keeping your existing models.
12
13
13
-
This extension works with raw Backbone. However, in order to take full advantage of React/Backbone
14
-
architecture you are encouraged to upgrade to `Backbone.NestedTypes`. It will give you following
14
+
# Breaking changes introduced in 0.3
15
+
-`component.createView( props )` doesn't work any more, use `new component.View( props )` instead.
16
+
- module and `npm` package name is now `nestedreact`.
17
+
- Raw `backbone` is not supported any more. Upgrade to `NestedTypes` 1.1.5 or more is required. It will give you following
15
18
features to help managing complex application state:
19
+
- Proper nested models and collections implementation with deep changes detection. React components will
20
+
update UI on nested attribute changes.
21
+
- Dramatic improvement in model update performance compared to Backbone. Up to 40x faster in Chrome. Important for mobile devices.
22
+
- Type safety. Attributes are guaranteed to hold values of declared types all the time.
16
23
17
-
- Proper nested models and collections implementation with deep changes detection. React components will
18
-
update UI on nested attribute changes.
19
-
- Dramatic improvement in model update performance compared to Backbone. Up to 40x faster in Chrome. Imprortant for mobile devices.
20
-
- Type safety. Attributes are guaranteed to hold values of declared types all the time.
21
-
22
-
For more information, visit
23
-
http://volicon.github.io/backbone.nestedTypes/
24
-
and
25
-
https://github.com/Volicon/backbone.nestedTypes
24
+
For more information about `NestedTypes`, visit
25
+
http://volicon.github.io/backbone.nestedTypes/
26
+
and
27
+
https://github.com/Volicon/backbone.nestedTypes
26
28
27
29
# Usage
28
-
It's packed as single UMD, thus grab the module which is appropriate for you. So far, there are two of them, one for raw backbone, and one for `backbone.nestedTypes`.
30
+
It's packed as single UMD, thus grab the module or use `npm` to install.
31
+
`npm install --save nestedreact`
29
32
30
-
First one depends on `react` and `backbone`, so if you're using Chaplin or Marionette you will
31
-
probably need to pass appropriate module instead of `backbone`. Don't hesitate to replace module name in the beginning of the file, or use raw factory function from `src/glue.js`.
33
+
Module export's modified React namespace (without touching original React), and its
34
+
safe to use it as a replacement for `react`.
32
35
33
-
If you're using `NestedTypes`, you need `NestedTypes` to require appropriate framework. Report a bug if something goes wrong, we like when someone share our passion for technology and quite fast in response.
36
+
If you're using backbone-based frameworks such as `ChaplinJS` or `Marionette`,
37
+
you need to do following things:
38
+
- Make sure that frameworks includes `nestedtypes` instead of `backbone`.
39
+
- On application start, tell `nestedreact` to use proper base class for View.
- New Model definition will be created, using `attributes` as Model.defaults.
146
+
- New `NestedTypes`Model definition will be created, using `attributes` as Model.defaults.
111
147
- If Model property is specified, it will be used as base model and extended.
112
148
-`attributes` property from mixins will be properly merged.
113
-
- if you're using `Backbone.NestedTypes` models, it's far superior to react state in every aspect. It handles updates much faster, it detects nested elements changes, and it has type specs for state elements in a way like react's `propTypes`.
149
+
- Since `state` is `NestedTypes` model in this case,
150
+
- All attributes *must* be declared using `NestedTypes` standard type specs.
151
+
- `state` attributes allows direct assignments - treat it as regular object.
152
+
- Every `state` modification (including direct assignments and nested attributes changes) will
153
+
cause automagical react update.
114
154
115
155
## Passing Backbone objects as React components props
116
156
```javscript
117
157
var MyComponent = React.createClass({
118
-
listenToProps : {
158
+
listenToProps : { // or just string with property names, separated by space
You can update react component on backbone events from component props.
137
-
138
177
Event subscription is managed automatically. No props passed - no problems.
178
+
179
+
## Data binding
180
+
181
+
`nestedreact` supports data binding links compatible with standard React's `valueLink`.
182
+
Links are "live" in a sense that they always point to actual value based on current model or collection state.
183
+
It doesn't break anything in React, rather extends possible use cases.
184
+
185
+
-`var link = model.getLink( 'attr' )` creates link for model attribute.
186
+
-`var link = collection.getLink( model )` creates boolean link, toggling model in collection. True if model is contained in collection, assignments will add/remove given model. Useful for checkboxes.
187
+
188
+
### Value access methods
189
+
190
+
In addition to standard members `link.requestChange( x )` and `link.value`, links supports all popular property access styles:
191
+
192
+
- jQuery property style: setter `link.val( x )`, getter `link.val()`
193
+
- Backbone style: setter `link.set( x )`, getter `link.get()`
-`link.toggle()` is a shortcut for `link.requestChange( !link.value )`
196
+
197
+
Most efficient way to work with link is using `link.val()` function, that's how its internally implemented. `val` function is bound, and can be passed around safely.
198
+
199
+
### Link transformations
200
+
201
+
Attribute's link can be further transformed using extended link API. Link transformations allowing you to use new `stateless functions` component definition style introduced in React 0.14 in most cases.
202
+
203
+
For links with any value type:
204
+
205
+
-`link.equals( x )` creates boolean link which is true whenever link value is equal to x. Useful for radio groups.
206
+
-`link.update( x => !x )` creates function transforming link value (toggling boolean value in this case). Useful for `onClick` event handlers.
207
+
208
+
For link enclosing array:
209
+
210
+
-`arrLink.contains( x )` creates boolean link which is true whenever x is contained in an array in link value. Useful for checkboxes. Avoid long arrays, currently operations has O(N^2) complexity.
211
+
212
+
For link enclosings arrays and plain JS objects:
213
+
-`arrOrObjLink.at( key )` creates link to array of object member with a given key. Can be applied multiple times to work with object hierarchies; on modifications, objects will be updated in purely functional way (modified parts will be shallow copied). Useful when working with plain JS objects in model attributes - updating them through links make changes visible to the model.
214
+
-`arrOrObjLink.map( ( itemLink, key ) => <input key={ key } valieLink={ itemLink } /> )` iterates through object or array, wrapping its elements to links. Useful for JSX transofrmation.
215
+
216
+
### Links and components state
217
+
218
+
Link received through component props can be mapped as state member using the following declaration:
219
+
```javascript
220
+
attributes : {
221
+
selected :Nested.link( '^props.selectedLink' )
222
+
}
223
+
```
224
+
It can be accessed as a part of state, however, in this case it's not true state. All read/write operations will be done with link itself, and local state won't be modified.
225
+
226
+
Also, links can be used to declaratively expose real component state to upper conponents. In this example, link optionally received in props will be updated every time `this.state.selected` object is replaced. In this case, updates are one way, from bottom component to upper one, and stateful component will render itself when state is changed.
0 commit comments