Skip to content

Instantly share code, notes, and snippets.

@barneycarroll barneycarroll/app.js forked from dontwork/app.js
Last active Mar 11, 2016

Embed
What would you like to do?
var m = require('mithril');
var layout = require('./components/layout/layout.js');
m.route.mode = 'pathname';
var Account = require('./models/account.model');
authRoutes(document.getElementById('app'), '/', layout.wrap({
'/': require('./pages/dashboard.js'),
'/login': require('./pages/login.js'),
'/contracts': require('./pages/contracts.js'),
'/contracts/:id': require('./pages/contractDashboard.js'),
'/contracts/:contractId/edit': require('./pages/contractEdit.js'),
'/contracts/:contractId/edit/:contractSectionId': require('./pages/contractSectionEdit.js'),
'/contracttemplates': require('./pages/contractTemplates.js'),
'/contracttemplates/:id': require('./pages/contractTemplateEdit.js'),
'/bundles': require('./pages/bundles.js'),
'/events': require('./pages/events.js'),
'/organisations': require('./pages/organisations.js'),
'/organisations/:id': require('./pages/organisationDashboard.js'),
'/organisations/:id/edit': require('./pages/organisationEdit.js'),
'/people': require('./pages/people.js'),
'/users': require('./pages/users.js'),
}));
function authRoutes(root, defaultRoute, router) {
for (var route in router) {
var component = router[route];
// We define a higher-level component for each route
router[route] = {
// View stays the same
view: component.view,
// Controller is only the same if component has a `skipAuth` flag
controller: component.skipAuth ? component.controller : function authController() {
// Auth is asynch, so stop rendering until the outcome is known
// (otherwise the view will render with an empty ctrl argument)
m.startComputation();
Account.identity().then(success, component.skipAuth ? success : failure);
function success() {
// Redirect to the original component, resume rendering
m.mount(root, component);
m.endComputation();
}
function failure() {
m.mount(root, router['/login']);
m.endComputation();
}
}
};
}
return m.route(root, defaultRoute, router);
}
var m = require('mithril');
var navbar = require('./navbar');
var sidebar = require('./sidebar');
var toast = require('./toast')
var modal = require('./modal')
var upgrade = require('../../helpers/upgrade.js');
var redirectUnauthorised = require('../../helpers/redirectUnauthorised.js')
var layout = {}
layout.controller = function (module) {
var ctrl = this;
redirectUnauthorised();
ctrl.controller = new module.controller
ctrl.view = module.view
}
layout.view = function (ctrl) {
return m('div', [
m(".mdl-layout.mdl-js-layout.mdl-layout--fixed-header", { config: upgrade }, [
m(navbar),
m(sidebar),
m(modal, { component: modal.content, key: modal.mode }),
m('main.mdl-layout__content', [
ctrl.view(ctrl.controller),
m(toast)
])
])
])
}
layout.wrap = function (routes) {
var map = {}
Object.keys(routes).map(function (r) {
map[r] = {
controller: function () {
return new layout.controller(routes[r])
},
view: layout.view
}
})
return map
}
module.exports = layout;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.