npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-react-component

v1.0.0

Published

Backbone.React.Component is a wrapper for React.Component and brings all the power of Facebook's React to Backbone.js

Downloads

198

Readme

Backbone.React.Component Build Status

Backbone.React.Component is a mixin and API that glues Backbone models and collections into React components.

When used as a mixin the component is mounted, a wrapper starts listening to models and collections changes to automatically set your component state and achieve UI binding through reactive updates.

Table of Contents generated with DocToc

Dependencies

How To

Using Bower

bower install backbone-react-component

Using Npm

npm install backbone-react-component

If you're not using Bower nor Npm download the source from the dist folder or use CDNJS.

API Usage

import Backbone from 'backbone';
import backboneReact from 'backbone-react-component';
import React from 'react';

var collection1 = new Backbone.Collection([
  {hello: 1},
  {hello: 2}
]);

export default class Component extends React.Component {
  componentWillMount () {
    backboneReact.on(this, {
      collections: {
        myCollection: collection1
      }
    });
  }

  componentWillUnmount () {
    backboneReact.off(this);
  }

  render () {
    return (
      <div>
        {this.state.myCollection.map((model) => model.hello)}
      </div>
    );
  }
}

Mixin Usage

One model

var MyComponent = React.createClass({
  mixins: [Backbone.React.Component.mixin],
  render: function () {
    return <div>{this.state.model.foo}</div>;
  }
});

var model = new Backbone.Model({foo: 'bar'});

ReactDOM.render(<MyComponent model={model} />, document.body);
// Update the UI
model.set('foo', 'Hello world!');

MyComponent will listen to any model changes, making the UI refresh.

One collection

var MyComponent = React.createClass({
  mixins: [Backbone.React.Component.mixin],
  createEntry: function (entry) {
    return <div key={entry.id}>{entry.helloWorld}</div>;
  },
  render: function () {
    return <div>{this.state.collection.map(this.createEntry)}</div>;
  }
});
var collection = new Backbone.Collection([
  {id: 0, helloWorld: 'Hello world!'},
  {id: 1, helloWorld: 'Hello world!'}
]);

ReactDOM.render(<MyComponent collection={collection} />, document.body);

Multiple models and collections

var MyComponent = React.createClass({
  mixins: [Backbone.React.Component.mixin],
  createEntry: function (entry) {
    return <div>{entry.helloWorld}</div>;
  },
  render: function () {
    return (
      <div>
        {this.state.firstModel.helloWorld}
        {this.state.secondModel.helloWorld}
        {this.state.firstCollection.map(this.createEntry)}
        {this.state.secondCollection.map(this.createEntry)}
      </div>
    );
  }
});

var MyFactory = React.createFactory(MyComponent);

var newComponent = MyFactory({
  model: {
    firstModel: new Backbone.Model({helloWorld: 'Hello world!'}),
    secondModel: new Backbone.Model({helloWorld: 'Hello world!'})
  },
  collection: {
    firstCollection: new Backbone.Collection([{helloWorld: 'Hello world!'}]),
    secondCollection: new Backbone.Collection([{helloWorld: 'Hello world!'}])
  }
});
ReactDOM.render(newComponent, document.body);

Usage on the server (Node.js)

var Backbone = require('backbone');
var backboneMixin = require('backbone-react-component');
var React = require('react');

var model = new Backbone.Model({
  helloWorld: 'Hello world!'
});
var HelloWorld = React.createClass({
  mixins: [backboneMixin],
  render: function () {
    return React.DOM.div({}, this.state.model.helloWorld);
  }
});
var HelloWorldFactory = React.createFactory(HelloWorld);

// Render to an HTML string
ReactDOM.renderToString(HelloWorldFactory({model: model}));
// Updating the model
model.set('helloWorld', 'Hi again!');
// Rendering to an HTML string again
ReactDOM.renderToString(HelloWorldFactory({model: model}));

API

on(component, modelsAndCollectionsObject)

Binds all models/collections found inside modelsAndCollectionsObject to component. modelsAndCollectionsObject takes the following form:

{
  models: {
    a: new Backbone.Model() // binds to `@state.a`
  },
  collections: {
    b: new Backbone.Collection() // binds to `@state.b`
  }
}

onModel(component, modelsObject)

Shortcut method to #on. modelsObject can either be an object of Backbone.Models or a single instance of one.

onCollection(component, collectionsObject

Shortcut method to #on. collectionsObject can either be an object of Backbone.Collections or a single instance of one.

off(component)

Teardown method. Unbinds all models and collections from component.

Mixin API

The following API is under Backbone.React.Component.mixin (require('backbone-react-component')):

$

Inspired by Backbone.View, it's a shortcut to this.$el.find method if jQuery is present, else it fallbacks to native DOM querySelector.

getCollection()

Grabs the component's collection(s) or from one of the parents.

getModel()

Grabs the component's model(s) or from one of the parents.

overrideModel()

Hook that can be implemented to return a model or multiple models. This hook is executed when the component is initialized. It's useful on cases such as when react-router is being used.

overrideCollection()

Hook that can be implemented to return a collection or multiple collections. This hook is executed when the component is initialized. It's useful on cases such as when react-router is being used.

Examples