Slide 1

Slide 1 text

Backbone & React The Hybrid App Peter Piekarczyk May 29, 2015

Slide 2

Slide 2 text

UI Engineer Trunk Club @peterpme github.com/ppiekarczyk

Slide 3

Slide 3 text

Getting Started

Slide 4

Slide 4 text

Past & Present Brunch With Panache A modular approach to building web apps with Brunch github.com/trunkclub/brunch-with-panache speakerdeck.com/jhabdas/brunch-with-panache • Backbone • Chaplin • JQuery • Brunch • Coffeescript • Automatic memory management • Controllers & composition • Rails-like routes in custom router • Consistent CLI interface • CommonJS + Amd Support

Slide 5

Slide 5 text

SOA BWP BWP

Slide 6

Slide 6 text

BWP BWP BWP BWP BWP

Slide 7

Slide 7 text

“Ugh, guys?” Everything worked great for a long time Until it stopped working that great.. • Numerous API calls & hydrations • Lot of useless re-rendering • Increased load times • Huge flame chart spikes • User complaints • Dedicating way too much time 
 for render debugging & optimization • Patches weren’t doing the trick What could UI solve on its own?

Slide 8

Slide 8 text

React to the Rescue! Replacing Backbone views with React Instant gratification • Reusable, encapsulated components • Efficient diff’ing algorithm re-renders only what it needs to • Declarative style makes it easier to focus on the app itself • Less mental overhead of bringing in templates & views together For the first time, our backend’s were much more comfortable with our code

Slide 9

Slide 9 text

React to the Rescue! Templates & Views Our code became much more manageable

Slide 10

Slide 10 text

React to the Rescue! CSS Styles as Components Our design & code became much more manageable

Slide 11

Slide 11 text

React to the Rescue! No Magic, Just Good Javascript.

Slide 12

Slide 12 text

Migration Process • Start with small components • Use a Backbone/React Mixin • Get familiar with the API • initialize = componentDidMount • remove = componentWillUnmount • Convert your parent view to use React.createElement instead Don’t freak out about the templates!

Slide 13

Slide 13 text

React Backbone Adapter 1. Create 2. Identify 3. Attach 4. Render 5. Dispose Attaching a React component to a Parent level Backbone view

Slide 14

Slide 14 text

React Backbone Adapter # Signup Controller Attaching a React component to a Parent level Backbone view

Slide 15

Slide 15 text

React Backbone Wrapper 1. Check for mount 2. Check for updates 3. Pass in the props 4. Find the node in the DOM 5. Apply an identifier class 6. Render!

Slide 16

Slide 16 text

signup-header.cjsx

Slide 17

Slide 17 text

Passing Down Data Multiple Models & Collections as Props

Slide 18

Slide 18 text

Passing Down Data What are Props? Properties that are received from above and are immutable
 as far as the component is concerned

Slide 19

Slide 19 text

Passing Down Data What is State? The state starts with a default value when a Component mounts and then suffers from mutations in time (mostly generated from user events).

Slide 20

Slide 20 text

Passing Down Data

Slide 21

Slide 21 text

Passing Down Data Backbone React Component Mixin Glue your Backbone models and collections into React components github.com/magalhas/backbone-react-component by José Magalhães

Slide 22

Slide 22 text

Passing Down Data Helper Methods @getModel() @getCollection() • @getModel().member.get(‘name’) • @getModel().address.set(‘zipcode’, 60714) • @getCollection().addresses.map (address, index) =>

Slide 23

Slide 23 text

Gotcha’s • CJSX - Babel is much more robust, if you can use it - use it.
 • Implicit returns - remember to wrap a series of components
 in a parent div, otherwise it won’t render 
 • CJSX - Event handlers will need to `return false`, otherwise 
 React will emit a warning Don’t freak out about the templates!

Slide 24

Slide 24 text

Final Thoughts • Interim 
 We don’t plan on building apps this way forever,
 our newer apps take a more robust Flux + React approach • React-Backbone by Joe Hudson
 github.com/jhudson8/react-backbone

Slide 25

Slide 25 text

Thank you