Last active
March 14, 2017 21:14
-
-
Save caseyWebb/b80de6b0c75c01abffa249fa1dc454f7 to your computer and use it in GitHub Desktop.
old router usage
This file contains 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
import ko from 'knockout' | |
import Router from 'ko-component-router' | |
ko.components.register('app', { | |
template: '<ko-component-router params="routes: routes"></ko-component-router>', | |
viewModel: class App { | |
constructor() { | |
this.routes = { | |
'/user/:id/!': [loadUser, 'user-show'] | |
} | |
} | |
} | |
}) | |
ko.components.register('user-show', { | |
template: ` | |
<h1 data-bind="text: user.name"></h1> | |
<ko-component-router params="routes: routes, user: user"></ko-component-router> | |
`, | |
viewModel: class { | |
constructor(ctx) { | |
this.user = ctx.user | |
this.routes = { | |
'/': 'user-overview', | |
'/todos': [loadUserToDos, 'user-todos'] | |
} | |
} | |
} | |
}) | |
ko.components.register('user-overview', { | |
template: ` | |
<span data-bind="text: user.foo"></span> | |
`, | |
viewModel: class { | |
constructor(ctx) { | |
this.user = ctx.user // via passthrough params | |
} | |
} | |
}) | |
ko.components.register('user-todos', { | |
template: ` | |
<ul data-bind="foreach: user.todos"> | |
... | |
</ul> | |
`, | |
viewModel: class { | |
constructor(ctx) { | |
this.todos = ctx.todos | |
} | |
} | |
}) | |
function loadUser(ctx) { | |
const p = $.get('/api/user', { id: ctx.params.id }) | |
p.then((user) => ctx.user = user) | |
ctx.queue(p) | |
} | |
function loadUserTodos(ctx) { | |
const p = $.get('/api/user/todos', { id: ctx.user.id }) // via passthrough | |
p.then((todos) => ctx.todos = todos) | |
ctx.queue(p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment