├── .gitignore ├── LICENSE.md ├── README.md ├── html ├── index.js └── raw.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | .sauce-credentials.json 4 | sauce_connect.log 5 | npm-debug.log* 6 | coverage.json 7 | .DS_Store 8 | *.swp 9 | .zuulrc 10 | package-lock.json 11 | yarn.lock 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Maciej Sitko 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## choo-detached 2 | [![npm version](https://badge.fury.io/js/choo-detached.svg)](https://badge.fury.io/js/choo-detached) 3 | 4 | A wrapper around choo API that allows to use choo components without a baked-in routing layer. 5 | 6 | [![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard) 7 | 8 | ## Example 9 | 10 | ```javascript 11 | var choo = require('choo-detached') 12 | var html = require('choo-detached/html') 13 | var log = require('choo-log') 14 | 15 | var app = choo() 16 | app.use(log()) 17 | app.use(countStore) 18 | app.component(mainView) 19 | app.mount('body') 20 | 21 | function mainView (state, emit) { 22 | return html` 23 | 24 |

count is ${state.count}

25 | 26 | 27 | ` 28 | 29 | function onclick () { 30 | emit('increment', 1) 31 | } 32 | } 33 | 34 | function countStore (state, emitter) { 35 | state.count = 0 36 | emitter.on('increment', function (count) { 37 | state.count += count 38 | emitter.emit('render') 39 | }) 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /html/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('choo/html') 2 | -------------------------------------------------------------------------------- /html/raw.js: -------------------------------------------------------------------------------- 1 | module.exports = require('choo/html/raw') 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert') 2 | const Choo = require('choo') 3 | 4 | function Detached (opts) { 5 | opts = opts || {} 6 | if (!(this instanceof Detached)) return new Detached(opts) 7 | Choo.call(this, opts) 8 | 9 | this._detached = opts.detached === undefined ? true : opts.detached 10 | 11 | if (this._detached) { 12 | this._historyEnabled = false 13 | this._hrefEnabled = false 14 | 15 | this.handler = null 16 | 17 | this._matchRoute = this._matchRoute.bind(this, '/') 18 | } 19 | } 20 | 21 | Detached.prototype = Object.create(Choo.prototype) 22 | Detached.prototype.constructor = Detached 23 | 24 | Detached.prototype.component = function (handler) { 25 | assert.notEqual(this._detached, false, 'choo: attempt to mount component in non-detached mode') 26 | assert.equal(typeof handler, 'function', 'choo: handler should be type function') 27 | 28 | this.route('/', handler) 29 | } 30 | 31 | module.exports = Detached 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "choo-detached", 3 | "version": "1.0.1", 4 | "description": "Detached (routerless) choo solution", 5 | "main": "index.js", 6 | "scripts": { 7 | "deps": "dependency-check ./package.json --entry index.js && dependency-check ./package.json --entry html/index.js", 8 | "test": "standard && npm run deps && node test.js" 9 | }, 10 | "keywords": [ 11 | "choo", 12 | "detached", 13 | "cool", 14 | "framework", 15 | "js", 16 | "spa" 17 | ], 18 | "author": "Maciej Sitko", 19 | "license": "ISC", 20 | "dependencies": { 21 | "choo": "^6.7.0", 22 | "assert": "^1.4.1" 23 | }, 24 | "devDependencies": { 25 | "dependency-check": "^2.9.1", 26 | "standard": "^10.0.3", 27 | "tape": "^4.8.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | 3 | var html = require('./html') 4 | var raw = require('./html/raw') 5 | var choo = require('./') 6 | 7 | tape('should render on the server', function (t) { 8 | var app = choo() 9 | app.component(function (state, emit) { 10 | var strong = 'Hello filthy planet' 11 | return html` 12 |

${raw(strong)}

13 | ` 14 | }) 15 | var res = app.toString('/') 16 | var exp = '

Hello filthy planet

' 17 | t.equal(res.toString().trim(), exp, 'result was OK') 18 | t.end() 19 | }) 20 | --------------------------------------------------------------------------------