Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

Commit

Permalink
fix #69 (pt 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Jun 13, 2016
1 parent 46d6864 commit c2d7af6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
6 changes: 5 additions & 1 deletion dist/ko-component-router.js
Expand Up @@ -444,7 +444,11 @@ return /******/ (function(modules) { // webpackBootstrap

var el = this.config.el.getElementsByClassName('component-wrapper')[0];
delete toCtx.query;
(0, _utils.extend)(this, toCtx);
if (fromCtx.route.component === toCtx.route.component) {
(0, _utils.merge)(this, toCtx);
} else {
(0, _utils.extend)(this, toCtx);
}
if (query) {
this.query.update(query, pathname);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/ko-component-router.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/dist/bundle.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/context.js
Expand Up @@ -168,7 +168,11 @@ var Context = function () {

var el = this.config.el.getElementsByClassName('component-wrapper')[0];
delete toCtx.query;
(0, _utils.extend)(this, toCtx);
if (fromCtx.route.component === toCtx.route.component) {
(0, _utils.merge)(this, toCtx);
} else {
(0, _utils.extend)(this, toCtx);
}
if (query) {
this.query.update(query, pathname);
}
Expand Down
8 changes: 6 additions & 2 deletions src/context.js
Expand Up @@ -2,7 +2,7 @@ import ko from 'knockout'
import qs from 'qs'
import { factory as queryFactory } from './query'
import { factory as stateFactory } from './state'
import { extend, isUndefined } from './utils'
import { extend, isUndefined, merge } from './utils'

export default class Context {
constructor(bindingCtx, config) {
Expand Down Expand Up @@ -128,7 +128,11 @@ export default class Context {
function complete(animate) {
const el = this.config.el.getElementsByClassName('component-wrapper')[0]
delete toCtx.query
extend(this, toCtx)
if (fromCtx.route.component === toCtx.route.component) {
merge(this, toCtx)
} else {
extend(this, toCtx)
}
if (query) {
this.query.update(query, pathname)
}
Expand Down
18 changes: 16 additions & 2 deletions test.js
Expand Up @@ -21,7 +21,7 @@ import './src'
ko.options.deferUpdates = true

test('ko-component-router', async (t) => { // eslint-disable-line
const NUM_TESTS_PER_SUITE = 60
const NUM_TESTS_PER_SUITE = 62
const NUM_CONFIGS = 4
const NUM_TESTS = NUM_TESTS_PER_SUITE * NUM_CONFIGS + 4
t.plan(NUM_TESTS)
Expand Down Expand Up @@ -102,14 +102,28 @@ async function runTests(t, config) {
t.equal(router.route().component, 'about', 'explicit path (and initialization)')
router.update('/user/casey/')
})
await step(() => {
await step((done) => {
t.equal(router.route().component, 'user', 'routes w/ missing optional parameter')
t.equal(router.params.name(), 'casey', 'attaches required param to ctx.params[PARAM_NAME]')
t.equal(router.params.operation(), undefined, 'optional param is `undefined` when missing')

const killMe = router.params.name.subscribe((n) => {
t.equal(n, 'barsh', 'ctx.params[PARAM_NAME] is subscribable')
killMe.dispose()
done()
})

router.update('/user/barsh/')
})
await step(() => {
router.update('/user/casey/edit')
})
await step(() => {
t.equal(router.params.operation(), 'edit', 'attaches optional param to ctx.params[PARAM_NAME]')
router.update('/user/barsh')
})
await step(() => {
t.equal(router.params.operation(), undefined, 'optional param on ctx.params[PARAM_NAME] gets set to undefined when navigated away from')
router.update('/this/page/does/not/exist')
})
await step(() => {
Expand Down

0 comments on commit c2d7af6

Please sign in to comment.