UNPKG

omniscient-tools

Version:
44 lines (38 loc) 1.1 kB
var react = require('react') var Promise = require('bluebird') var omniscientComponent = require('omniscient') // allows you to define a handler for react router exports.handler = function(urlLoad, render) { if (!render) { render = urlLoad; urlLoad = null; } var cache = {}; return React.createClass({ displayName: 'OmniscientToolsHandler', statics: { urlLoad: urlLoad, cache: cache }, render: function () { return render(cache.val); } }) } exports.loadHandlers = function(state, oldState) { var loads = state.routes .filter(function(route) { return route.handler.urlLoad }) .map(function(route) { route.handler.cache.val = null; return route.handler.urlLoad(state.params, state, oldState).then(function (val) { route.handler.cache.val = val; }); }); return Promise.all(loads) } // automatically use the jsx version. This is optional, just use normal omniscient // if you prefer the other way exports.component = function() { var comp = omniscientComponent.apply(null, arguments) return comp.jsx }