├── .gitignore ├── .npmignore ├── .travis.yml ├── test ├── mocha.opts ├── diez.container.js ├── diez.state.js ├── diez.has.js ├── diez.contains.js ├── diez.lookup.js ├── diez.get.js ├── diez.resolve.js └── diez.register.js ├── index.js ├── examples └── express │ ├── screenshot.png │ ├── server.js │ ├── README.md │ ├── routes.js │ ├── layout.html │ ├── app.js │ └── handlers │ ├── app.js │ └── welcome.js ├── lib ├── core.js ├── diez.js ├── scalar.js ├── factory.js └── api.js ├── LICENSE ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .travis.yml 2 | examples 3 | LICENSE 4 | test 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 0.10 5 | - 0.11 6 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers js:6to5/register 2 | --reporter spec 3 | --ui bdd 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var diez = require('./lib/diez'); 2 | 3 | module.exports = diez(); // Root container 4 | -------------------------------------------------------------------------------- /examples/express/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericclemmons/diez/HEAD/examples/express/screenshot.png -------------------------------------------------------------------------------- /lib/core.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash'); 2 | var assert = require('assert'); 3 | var stampit = require('stampit'); 4 | 5 | module.exports = stampit().methods({ 6 | matches: function() { 7 | throw new Error('Stamp is missing `.matches` function'); 8 | }, 9 | 10 | resolve: function() { 11 | throw new Error('Stamp is missing `.resolve` function'); 12 | }, 13 | }); 14 | -------------------------------------------------------------------------------- /examples/express/server.js: -------------------------------------------------------------------------------- 1 | require("6to5/register")({ 2 | only: /examples/ 3 | }); 4 | 5 | var app = require('./app'); 6 | var diez = require('../../'); 7 | var util = require('util'); 8 | 9 | app.set('port', process.env.PORT || 3000); 10 | 11 | app.listen(app.get('port'), function() { 12 | console.info('Express server listening on port %s (%s)', app.get('port'), app.get('env')); 13 | }); 14 | -------------------------------------------------------------------------------- /examples/express/README.md: -------------------------------------------------------------------------------- 1 | # Server-side React with Diez & Express 2 | 3 | > ![](screenshot.png) 4 | 5 | ## [Demo][4] ([source][5]) 6 | 7 | 8 | ## Usage 9 | 10 | Run: 11 | 12 | ``` 13 | .../examples/express $ node server.js 14 | ``` 15 | 16 | Open: 17 | 18 | > 19 | 20 | [4]: https://protected-castle-7387.herokuapp.com/ 21 | [5]: https://github.com/ericclemmons/diez/tree/master/examples/express 22 | -------------------------------------------------------------------------------- /lib/diez.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash'); 2 | var stampit = require('stampit'); 3 | var api = require('./api'); 4 | var version = require('../package.json').version; 5 | 6 | var diez = stampit.compose(api).state({ 7 | id: null, 8 | parent: null, 9 | version: version, 10 | }).enclose(function() { 11 | this.id = _.uniqueId('diez_'), 12 | 13 | this.container = function() { 14 | return diez({ parent: this }); 15 | }; 16 | 17 | this.register('container', this); 18 | }); 19 | 20 | module.exports = diez; 21 | -------------------------------------------------------------------------------- /examples/express/routes.js: -------------------------------------------------------------------------------- 1 | var diez = require('../../'); 2 | var React = require('react'); 3 | 4 | var App = require('./handlers/app'); 5 | var Welcome = require('./handlers/welcome'); 6 | 7 | var { 8 | Route, 9 | DefaultRoute 10 | } = require('react-router'); 11 | 12 | var Routes = function(App, Welcome) { 13 | return ( 14 | 15 | 16 | 17 | ); 18 | }; 19 | 20 | diez.register(Routes, [App, Welcome]); 21 | 22 | module.exports = Routes; 23 | -------------------------------------------------------------------------------- /lib/scalar.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash'); 2 | var assert = require('assert'); 3 | var core = require('./core'); 4 | var stampit = require('stampit'); 5 | 6 | module.exports = stampit().compose(core).state({ 7 | name: null, 8 | value: null, 9 | }).methods({ 10 | matches: function(reference) { 11 | return this.name === reference; 12 | }, 13 | 14 | resolve: function() { 15 | return this.value; 16 | }, 17 | }).enclose(function() { 18 | assert(_.isString(this.name)); 19 | assert(!_.isNull(this.value) && !_.isUndefined(this.value)); 20 | }); 21 | -------------------------------------------------------------------------------- /examples/express/layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /test/diez.container.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var diez = require('..'); 3 | 4 | describe('.container', function() { 5 | beforeEach(function() { 6 | this.container = diez.container(); 7 | }); 8 | 9 | it('should set `parent`', function() { 10 | assert(this.container.parent); 11 | }); 12 | 13 | it('should be a child of the parent', function() { 14 | assert.equal(this.container.parent, diez); 15 | }); 16 | 17 | it('should have a \'container\' alias that references itself', function() { 18 | assert(this.container.contains('container')); 19 | assert.equal(this.container.get('container'), this.container); 20 | }); 21 | }); 22 | 23 | -------------------------------------------------------------------------------- /test/diez.state.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var diez = require('..'); 3 | 4 | describe('state', function() { 5 | it('should default to a `null` parent', function() { 6 | assert.equal(null, diez.parent); 7 | }); 8 | 9 | it('should set a version', function() { 10 | assert(diez.version); 11 | }); 12 | 13 | it('should have an id', function() { 14 | assert(diez.id, 'diez_1'); 15 | }); 16 | 17 | describe('in a new container', function() { 18 | beforeEach(function() { 19 | this.container = diez.container(); 20 | }); 21 | 22 | it('should have an id', function() { 23 | assert(this.container.id, 'diez_2'); 24 | }); 25 | 26 | it('should have a different id', function() { 27 | assert(this.container.id, diez.id); 28 | }); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Internet Systems Consortium (ISC) license 2 | ========================================= 3 | 4 | Copyright (c) 2015 Eric Clemmons 5 | 6 | Permission to use, copy, modify, and/or distribute this software for any purpose 7 | with or without fee is hereby granted, provided that the above copyright notice 8 | and this permission notice appear in all copies. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 11 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 12 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 13 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 14 | OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 15 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 16 | THIS SOFTWARE. 17 | -------------------------------------------------------------------------------- /examples/express/app.js: -------------------------------------------------------------------------------- 1 | var diez = require('../../'); 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var React = require('react'); 5 | var Router = require('react-router'); 6 | var util = require('util'); 7 | 8 | var app = module.exports = require('express')(); 9 | var layout = fs.readFileSync(path.join(__dirname, 'layout.html'), 'utf8'); 10 | var Routes = require('./routes'); 11 | 12 | app 13 | .use(function(req, res, next) { 14 | req.container = diez.container(); 15 | req.container.register('request', req); 16 | 17 | next(); 18 | }) 19 | .get('/', function(req, res, next) { 20 | Router.create({ 21 | routes: req.container.get(Routes), 22 | location: req.url 23 | }).run(function(Handler) { 24 | var element = React.createElement(Handler); 25 | var content = React.renderToStaticMarkup(element); 26 | var noscript = /