• Stars
    star
    840
  • Rank 54,265 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Plugin for React to make Backbone migration easier

react.backbone

Build Status

Plugin for React to make Backbone integration easier. Initialize your component with a Backbone.Model or Backbone.Collection; when the model or collection changes, #render will be called.

var UserViewComponent = React.createBackboneClass({
    changeOptions: "change:name", // DEFAULT is "change",
    render: function() {
        return (
          <div>
              <h1>{this.getModel().get("name")}</h1>
          </div>
        );
    }
});

var user = new Backbone.Model();
var UserView = React.createFactory(UserViewComponent);
var userView = UserView({model: user});

// Mount your component directly
React.render(userView, document.getElementById('element'));

// Render as a subview
var ProfileViewComponent = React.createClass({
  render: function() {
      return (
        <div>
          <UserViewComponent model={this.props.user} />
        </div>
      );
  }
});

var ProfileView = React.createFactory(ProfileViewComponent);
var profileView = ProfileView({user: user});
React.render(profileView, document.getElementById('element'));

React.Backbone also plays nicely with Backbone.Collection. Anytime the add, remove, reset or sort events are triggered the component will re-render.

var UserViewComponent = React.createBackboneClass({
  render: function() {
    return <li>{ this.getModel().get('name') }</li>;
  }
});

var UsersListViewComponent = React.createBackboneClass({
    render: function() {
        var usersList = this.getCollection().map(function(user) {
            return <UserViewComponent model={user} />;
        });

        return (
          <div>
            <ul>
              { usersList }
            </ul>
          </div>
        );
    }
});

var usersList = new Backbone.Collection();
var UsersListView = React.createFactory(UsersListViewComponent);
var usersListView = UsersListView({collection: usersList});

React.render(usersListView, document.getElementById('users'));

If you need to use multiple models, you can do so by including the mixin multiple times:

var CommentViewComponent = React.createBackboneClass({
    mixins: [

        // when the view is instantiated,
        // 'user' and 'comment' can be passed as props
        React.BackboneMixin("user", "change:name"),
        React.BackboneMixin("comment")
    ],
    render: function() {
        return (
          <div>
              <p>{this.props.comment.get("text")}</p>
              <p>{'posted by' + this.props.user.get("name")}</h1>
          </div>
        );
    }
});

var user = new Backbone.Model();
var comment = new Backbone.Model();
var CommentView = React.createFactory(CommentViewComponent);
var commentView = CommentView({user: user, comment: comment});

React.render(usersListView, document.getElementById('users'));

You can also pass an object with options to the included mixin:

React.BackboneMixin({
    propName: "user",
    renderOn: "change:name"
});

Or supply a custom callback to the option modelOrCollection to retrieve the property from the component's props:

React.BackboneMixin({
    modelOrCollection: function(props) {
        return props.comment.user;
    },
    renderOn: "change:name"
});

If your Collection or Model class does not inherit directly from Backbone.Model or Backbone.Collection, you may customize the behavior on a library level by overriding the React.BackboneMixin.ConsiderAsCollection function.

Return true if the object passed should behave as a collection.

React.BackboneMixin.ConsiderAsCollection = function (modelOrCollection) {
  return modelOrCollection instanceof Backbone.Collection || modelOrCollection instanceof MyCustomCollection;
}

Installation

Either download react.backbone.js or install the react.backbone package on Bower:

bower install --save react.backbone

You can either include react.backbone in a <script> tag (after you've included Backbone and React) or through RequireJS/AMD:

define(['backbone', 'react', 'react.backbone'], function(Backbone, React) {
    var UserViewComponent = React.createBackboneClass({
        // ...
    });
});

Version 0.7.0 supports React >0.14 and Backbone >1.0; for React <0.14, check out versions 0.6.0 and prior. React Native is also supported, but only works with npm v3 (otherwise it will use the wrong React dependency).

More Repositories

1

routable-ios

Routable, an in-app native URL router, for iOS
Objective-C
1,815
star
2

formotion

Making iOS Forms insanely great with RubyMotion
Ruby
609
star
3

routable-android

Routable, an in-app native URL router, for Android
Java
474
star
4

graphqlhub

The GraphQLHub Server
JavaScript
402
star
5

ShotBlocker

Detecting iOS screenshots ala Snapchat and Facebook Poke
Objective-C
257
star
6

rubymotion-tutorial

RubyMotion-Tutorial
Ruby
211
star
7

Walt

Fast, frictionless iOS animations
Ruby
144
star
8

HybridKit-iOS

HybridKit for iOS
Objective-C
126
star
9

remote_model

JSON API <-> NSObject via RubyMotion
Ruby
97
star
10

geomotion

Better iOS Geometry with RubyMotion
Ruby
90
star
11

Routable

A RubyMotion UIViewController -> URL router
Ruby
86
star
12

graphql-intro

JavaScript
86
star
13

react-vr

Write VR web apps with React JS
JavaScript
83
star
14

motion-toolbox.com

The Motion-Toolbox website
CSS
79
star
15

relay-101

Relay 101
JavaScript
48
star
16

HybridKit-JS

HybridKit for JavaScript
JavaScript
45
star
17

motion-screenshots

Automatic screenshots for your RubyMotion apps
Ruby
41
star
18

twittermotion

RubyMotion wrapper for the iOS Twitter API
Ruby
40
star
19

readme-score

Giving a score to README.md's everywhere
Ruby
40
star
20

motion-smarticons

Automatically generate icon labels for RubyMotion apps
Ruby
39
star
21

maglev

Faster, smoother iOS development
Ruby
36
star
22

motion-require

Dependency management for RubyMotion, using a pseudo `require`
Ruby
34
star
23

motion-env

Sync ENV variables between Rakefile and RubyMotion
Ruby
31
star
24

applebot

Automating iTunes Connect and the iOS Developer Center
JavaScript
31
star
25

react-native-remote-component

Load React Native components over the network
Objective-C
27
star
26

VolumeCamera

iPhone app where camera is triggered by volume buttons
Objective-C
26
star
27

urlp

A command-line URL parser, written in Rust
Rust
22
star
28

CLAFluxDispatcher

A port of Facebook's Flux Dispatcher to Objective-C
Objective-C
22
star
29

mixing-objc-ruby

Mixing Objective-C and Ruby with RubyMotion
Objective-C
20
star
30

typescript-1.8-tutorial

Migrating a JavaScript app to TypeScript 1.8
JavaScript
20
star
31

motion-colorize

Add some color to your RubyMotion output
Ruby
17
star
32

motion-screenspecs

Test your RubyMotion app using screenshot comparison
Ruby
17
star
33

graphql-unions-interfaces

JavaScript
13
star
34

appleload

CLI & Ruby Library to control Apple's Application Loader app
Ruby
12
star
35

motion-weakattr

weak_attr for WeakRefs in RubyMotion
Ruby
10
star
36

appleslice

Easily parse Apple & iTunes Connect emails
Ruby
7
star
37

motion-http

Cross-platform HTTP library for RubyMotion
7
star
38

BrocabularyTutorial

The source to the Android App From Scratch tutorials
Java
7
star
39

relay-102

Relay with Mutations! Powered by GraphQLHub
JavaScript
6
star
40

plottable-examples

HTML
5
star
41

afmotion-rocket

AFMotion extensions for Rocket
Ruby
5
star
42

CYStoreExample

Magically easy in-app purchases on iOS.
Objective-C
4
star
43

MotionCorePlot

Ruby
3
star
44

graphql-directives

JavaScript
3
star
45

motion-tips

Motion Tips blog dump.
Ruby
3
star
46

gumroad-ruby

Ruby binding for Gumroad API
Ruby
3
star
47

smashing-rubymotion

Source code to Smashing's RubyMotion example
Ruby
3
star
48

droidproj

Manage Android projects with a Droidfile
Ruby
2
star
49

readme-score-api

The API for readme-score
Go
2
star
50

FormotionTest

Tests
Ruby
2
star
51

WebActivity

Drop-in Activity to browse the web
Java
1
star
52

new-news

TypeScript
1
star
53

botlib

The shared library for AppleBot and PlayBot
Ruby
1
star
54

clayallsopp.github.com

1
star