choo-lazy-view
Advanced tools
Comparing version 0.1.0 to 0.2.0
21
index.js
@@ -13,2 +13,3 @@ var assert = require('assert') | ||
assert(typeof fn === 'function', 'choo-lazy-view: fn should be a function') | ||
fn = promisify(fn) | ||
@@ -67,1 +68,21 @@ var Self = this | ||
} | ||
// wrap callback function with promise | ||
// fn -> fn | ||
function promisify (fn) { | ||
return function (cb) { | ||
return new Promise(function (resolve, reject) { | ||
var res = fn(function (err, value) { | ||
if (err) reject(err) | ||
else resolve(value) | ||
}) | ||
if (res instanceof Promise) return res.then(resolve, reject) | ||
}).then(done.bind(null, null), done) | ||
function done (err, res) { | ||
if (typeof cb === 'function') cb(err, res) | ||
if (err) throw err | ||
else return res | ||
} | ||
} | ||
} |
{ | ||
"name": "choo-lazy-view", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Lazily fetch view when needed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ # choo-lazy-view | ||
Lazily load view views as they are invoked by the router. Built for [split-require][split-require] but sohuld work with any software with a similar callback signature. | ||
Lazily load views as the router invokes them. Built for [split-require][split-require] but should work with any software with a similar callback signature. | ||
@@ -41,3 +41,3 @@ ## Usage | ||
### `LazyView.create(callback, loader?)` | ||
Accepts a callback and an optional loader view. The callback will be invoked when the returned function is called upon (called immediately in node). The callback in turn should load the required view and relay it's response (or error) back to the caller. | ||
Accepts a callback and an optional loader view. The callback will be invoked when the returned function is called upon (called immediately in node). The callback, in turn, should load the required view and relay it's response (or error) back to the caller. | ||
@@ -50,3 +50,3 @@ ```javascript | ||
The second argument is optional and should be a function or a DOM node which will be displayed while loading. By default the node used to mount the application in the DOM is used as loader (meaning the view remians unchanged while loading). | ||
The second argument is optional and should be a function or a DOM node which will be displayed while loading. By default, the node used to mount the application in the DOM is used as loader (meaning the view remains unchanged while loading). | ||
@@ -61,3 +61,3 @@ ```javascript | ||
### `LazyView.mount(app, selector)` | ||
Wrapper function for `app.mount`. Returns a promise which resolves once the application is ready. This is needed because split-require resolves modules asyncronously. | ||
Wrapper function for `app.mount`. Returns a promise which resolves once the application is ready. Needed because split-require resolves modules asynchronously | ||
@@ -72,3 +72,3 @@ ```javascript | ||
### Extending LazyView | ||
You may extend on LazyView to add a shared framework wrapper, e.g. a header, footer etc. | ||
You may extend on LazyView to add a shared framework wrapper, e.g. a header, footer, etc. | ||
@@ -115,3 +115,3 @@ ```javascript | ||
#### `choo-lazy-view:done` | ||
When view has been fetched and about to be rendered. | ||
When the view has been fetched and is about to rerender. | ||
@@ -118,0 +118,0 @@ [choo]: https://github.com/choojs/choo |
7553
72