Wondering what’s next for npm?Check out our public roadmap! »

This version has been deprecated

Author message:

this package has been deprecated

diezTypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Diez https://img.shields.io/npm/v/diez.svg

Incredibly simple, Dependency Injection for isomorphic Javascript applications.

The Problem

When using libraries such as React, React Router, & RefluxJS, you'll find that singletons don't work well on the server-side for Actions, Routes, Stores, & Views, as they share state between all requests.

On the client-side, each user's browser serves as a container to scope functions. Similarly, Diez introduces a container for each request on the server.

The Solution

Luckily, all it takes is to turn your singletons into factories by wrapping them with function(...) { return ...; } & registering them via diez.register.

Diez will retrieve them while isolating instances to a single container-per-request.


Demo


Getting Started

Suppose you're rendering your React view on the server with React Router, but rely on request-specific data, such as the user's ip, for some reason.

Step 0 - Install Diez

$ npm install --save diez

Step 1 - Create a container per request

In your application middleware:

// server.js
app.use(function(req, res, next) {
  // This container & it's references are sandboxed to this request.
  req.container = diez.container();
  req.container.register('request', req);
});

Now, every call to req.container.get('request') returns req.

Step 2 - Inject React components with dependencies

Before, your view is a singleton:

// MyView.js
var MyView = React.createClass({
  render: function() {
    <p>
      Hello!
    </p>
  }
});
 
module.exports = MyView;

After, your view is a factory with dependencies defined:

// MyView.js
var diez = require('diez');
 
// Convert singleton to factory
var MyView = function(request) {
  return React.createClass({
    render: function() {
      <p>
        Hello, {request.ip}!
      </p>
    }
  });
};
 
// Register component with dependencies
diez.register(MyView, ['request']);
 
module.exports = MyView;

Step 3 - Retrieve injected components

app.get('/', function(req, res) {
  // Get instantiated MyView with dependencies injected
  var component = req.container.get(MyView);
  var element   = React.createElement(view);
 
  res.send(React.renderToString(element));
});

That's it!


License

The MIT License (MIT)

Copyright (c) 2015 Eric Clemmons

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

none

Install

npm i [email protected]

Version

2.0.1

License

SEE LICENSE.md

Homepage

diez.org

Last publish

Collaborators

  • avatar