Last active
July 12, 2016 18:17
-
-
Save WreckedAvent/61a92d8fa2c12eda261161ed1f360ae6 to your computer and use it in GitHub Desktop.
simple strategy for showing different mithril views based on permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// some service to get the user's permissions | |
import * as user from './user-service' | |
import * as m from 'mithril' | |
const adminView = (ctrl) => m('.container.admin', [ | |
m('h1', 'Hello admin') | |
// show admin specific controls | |
]) | |
const moderatorView = (ctrl) => m('.container.moderator', [ | |
m('h1', 'Hello moderator') | |
// show moderator-specific controls | |
]) | |
const userView = (ctrl) => m('.container.user', [ | |
m('h1', 'Hello user') | |
// show user-specific controls | |
]) | |
const loadingView = (ctrl) => m('.container.loading', [ | |
m('.loading-spinner') | |
]) | |
const component = { | |
controller: () => { | |
const permissions = m.prop('') | |
user.getPermissions().then(perm => { | |
permissions(perm) | |
m.redraw() | |
}) | |
return { permissions } | |
}, | |
view: (ctrl) => { | |
const permissions = ctrl.permissions() | |
if (permissions === 'admin') return adminView(ctrl) | |
if (permissions === 'moderator') return moderatorView(ctrl) | |
if (permissions === 'user') return userView(ctrl) | |
return loadingView(ctrl) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment