Skip to content

Commit 1243242

Browse files
author
Vlad Balin
committed
Updated
1 parent b07ea87 commit 1243242

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/BackboneViews.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
There are many different ways you may approach the problem when dealing with existing Bakcbone UI.
44
All of them are supported, enabling easy and gradual transition to React.
55

6+
Though `NestedReact` offers excellent convergence layer for backbone views, raw backbone models are not supported.
7+
To use it for smooth migration of existing backbone application to React, you need to replace `backbone` with `NestedTypes`
8+
first (it's mostly backward compatible with Backbone 1.2 API, so transition is not hard).
9+
Which by itself will be a big step forward, because:
10+
- It's order of magnitude faster, so your application becomes more responsive and you can handle collection which are 10 times larger than you have now. [No kidding](http://slides.com/vladbalin/performance#/).
11+
- It implements nested models and collections handling in the right way. During `fetch`, nested objects are updated in place, so it's safe to pass them by reference.
12+
- It can handle model references by `id` in attributes for you too, operating on a set of independently fetched collections.
13+
- It's type-safe, providing the same contract for model attributes as in statically typed language. Thus,
14+
attributes are guaranteed to hold values of declared types whatever you do, making it impossible to break client-server protocol.
15+
- At the moment of writing, no other traditional model framework supports React's pure render optimization. :)
16+
17+
For more information about `NestedTypes`, visit
18+
http://volicon.github.io/backbone.nestedTypes/
19+
and
20+
https://github.com/Volicon/backbone.nestedTypes
21+
622
## Interoperation with existing Backbone Views
723

824
Key factor of success for technology transition project is to avoid naive 'upfront rewrite' strategy.
@@ -57,13 +73,13 @@ Occasionally, you may decide to refactor your existing View to React component.
5773

5874
Since Backbone generally use the same architectural concept as React (detect change and then render), it's an easy process.
5975
First of all, short vocabulary:
60-
1. View.extend({}) -> React.createClass({}). That's an obvious part.
61-
2. View.template -> Component.render(). Yeah. In React, `render` function just *returns* markup.
62-
3. View.render -> Component.forceUpdate(). And if you want to update component, you should call this thingy instead.
63-
4. View.render -> Component.componentDidUpdate(), Component.componentDidMount(). If you want to attach jQuery plugin after render, you do it here.
64-
5. View.initialize( options ) -> View.componentWillMount()
65-
6. View options you receive in (4) -> Component.props
66-
7. View.model -> Component.state
76+
1. `View.extend({})` -> `React.createClass({})`. That's an obvious part.
77+
2. `View.template` -> `Component.render()`. Yeah. In React, `render` function just *returns* markup.
78+
3. `View.render` -> `Component.forceUpdate()`. And if you want to update component, you should call this thingy instead.
79+
4. `View.render` -> `Component.componentDidUpdate()`, `Component.componentDidMount()`. If you want to attach jQuery plugin after render, you do it here.
80+
5. `View.initialize( options )` -> `View.componentWillMount()`
81+
6. `View options you receive in (4)` -> `Component.props`
82+
7. `View.model` -> `Component.state`
6783

6884
You approach the refactoring process in sequence:
6985
1. Create an empty React component.
@@ -77,9 +93,9 @@ must completely define an appearance of your markup. Since for Backbone it's not
7793
View's state model.
7894

7995
In Backbone, you might assign values from `options` to the model. Do not do this with React. Remember, `options` is `props`.
80-
Therefore, it might be required to remove some items from the View's model.
96+
Therefore, it might be required to remove some items from the View's model.
8197

82-
### Use Existing Backbone Model as component's state
98+
### Use Existing Model as component's state
8399

84100
If you already have one model, describing View's state (usual pattern is listening to model's `change` event and calling `this.render()`),
85101
you can just attach it to you React component. Like this. It will be created, disposed, and listened to automatically.
@@ -111,7 +127,7 @@ var MyComponent = React.createClass({
111127
- You can customize UI update events supplying `listenToState` property. For example, `listenToState : 'change:attr sync'`.
112128
- You can disable UI updates on state change, supplying `listenToState : false` option.
113129

114-
## Passing Backbone objects as React components props
130+
## Passing Models and Collections as React components props
115131

116132
In backbone, you might listen to models and collection changes which comes from the View `options`.
117133

@@ -155,7 +171,7 @@ var MyComponent = React.createClass({
155171

156172
That's simple and safe. No props passed - no events subscription.
157173

158-
## Helper methods for easy Backbone to React refactoring
174+
## Helper methods for easy Backbone View refactoring
159175

160176
When you will copy over your event handlers, most likely, they will just work.
161177

0 commit comments

Comments
 (0)