You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Casey Webb edited this page Dec 18, 2016
·
7 revisions
constcomponentSetterMiddleware=(ctx)=>{Object.defineProperty(ctx,'component',{set(component){ko.components.register(ctx.canonicalPath,component)ctx.route.component=ctx.canonicalPath}})}consttitleSetterMiddleware=(ctx)=>{Object.defineProperty(ctx,'title',{set(title){document.title=title}})}consthomeView=(ctx)=>{ctx.title='Home'ctx.component={template: '<h1>Welcome to my App!</h1>'}}ko.router.use(componentSetterMiddleware)ko.router.use(titleSetterMiddleware)ko.applyBindings({routes: {'/': homeView}})
or, with a little fp magic...
import{assign,curryRight}from'lodash'constassignFrom=(...srcs)=>curryRight(assign,srcs.length+1)(...srcs)constcreateSetterMiddleware=(prop,middleware)=>(ctx)=>{Object.defineProperty(ctx,prop,{set: (v)=>middleware(v,ctx)})}constcomponentSetterMiddleware=createSetterMiddleware('component',(c,ctx)=>{ko.components.register(ctx.canonicalPath,component)ctx.route.component=ctx.canonicalPath})consttitleSetterMiddleware=createSetterMiddleware('title',(t)=>{document.title=t})consthomeView=assignFrom({title: 'Home',component: {template: '<h1>Welcome to my App!</h1>'}})ko.router.use(componentSetterMiddleware)ko.router.use(titleSetterMiddleware)ko.applyBindings({routes: {'/': homeView}})