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

Commit

Permalink
fix query default bug (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Jul 18, 2016
1 parent 03e69da commit 6d18b10
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 14 deletions.
35 changes: 32 additions & 3 deletions dist/ko-component-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ return /******/ (function(modules) { // webpackBootstrap
var push = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
var query = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];

// debugger
var url = this.resolveUrl(origUrl);
var route = this.getRouteForUrl(url);
var firstRun = this.route() === '';
Expand Down Expand Up @@ -1166,7 +1167,6 @@ return /******/ (function(modules) { // webpackBootstrap

if (!cache[guid][prop]) {
cache[guid][prop] = {
defaultVal: defaultVal,
parser: parser,
value: _knockout2.default.pureComputed({
read: function read() {
Expand Down Expand Up @@ -1201,6 +1201,18 @@ return /******/ (function(modules) { // webpackBootstrap
})
};
}

if (defaultVal) {
// clone to prevent defaultVal from being changed by reference
if ((0, _utils.isArray)(defaultVal)) {
cache[guid][prop].defaultVal = defaultVal.slice(0);
} else if ((0, _utils.isPlainObject)(defaultVal)) {
cache[guid][prop].defaultVal = (0, _utils.extend)({}, defaultVal, false);
} else {
cache[guid][prop].defaultVal = defaultVal;
}
}

return cache[guid][prop].value;
}
}, {
Expand Down Expand Up @@ -1242,7 +1254,7 @@ return /******/ (function(modules) { // webpackBootstrap
var guid = this.ctx.config.depth + pathname;
for (var pn in cache[guid]) {
var p = cache[guid][pn];
p.value(p.defaultVal);
this.get(pn)(p.defaultVal);
}
}
}, {
Expand Down Expand Up @@ -1357,6 +1369,9 @@ return /******/ (function(modules) { // webpackBootstrap
exports.extend = extend;
exports.identity = identity;
exports.isUndefined = isUndefined;
exports.isFunction = isFunction;
exports.isPlainObject = isPlainObject;
exports.isArray = isArray;
exports.mapKeys = mapKeys;
exports.merge = merge;

Expand Down Expand Up @@ -1507,11 +1522,13 @@ return /******/ (function(modules) { // webpackBootstrap
}
} else if (isUndefined(src[prop])) {
dest[prop] = undefined;
} else if (src[prop].constructor === Object) {
} else if (isPlainObject(src[prop])) {
if (_shallow) {
dest[prop] = {};
}
extend(dest[prop], src[prop], createAsObservable);
} else if (isArray(src[prop])) {
dest[prop] = src[prop].slice(0);
} else {
dest[prop] = src[prop];
}
Expand Down Expand Up @@ -1542,6 +1559,18 @@ return /******/ (function(modules) { // webpackBootstrap
return typeof x === 'undefined';
}

function isFunction(x) {
return typeof x === 'function';
}

function isPlainObject(x) {
return x.constructor === Object;
}

function isArray(x) {
return isFunction(x.splice);
}

function mapKeys(obj, fn) {
var mappedObj = {};
Object.keys(obj).forEach(function (k) {
Expand Down
2 changes: 1 addition & 1 deletion dist/ko-component-router.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions 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.

1 change: 1 addition & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var Context = function () {
var push = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
var query = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];

// debugger
var url = this.resolveUrl(origUrl);
var route = this.getRouteForUrl(url);
var firstRun = this.route() === '';
Expand Down
15 changes: 13 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ var Query = function () {

if (!cache[guid][prop]) {
cache[guid][prop] = {
defaultVal: defaultVal,
parser: parser,
value: _knockout2.default.pureComputed({
read: function read() {
Expand Down Expand Up @@ -97,6 +96,18 @@ var Query = function () {
})
};
}

if (defaultVal) {
// clone to prevent defaultVal from being changed by reference
if ((0, _utils.isArray)(defaultVal)) {
cache[guid][prop].defaultVal = defaultVal.slice(0);
} else if ((0, _utils.isPlainObject)(defaultVal)) {
cache[guid][prop].defaultVal = (0, _utils.extend)({}, defaultVal, false);
} else {
cache[guid][prop].defaultVal = defaultVal;
}
}

return cache[guid][prop].value;
}
}, {
Expand Down Expand Up @@ -138,7 +149,7 @@ var Query = function () {
var guid = this.ctx.config.depth + pathname;
for (var pn in cache[guid]) {
var p = cache[guid][pn];
p.value(p.defaultVal);
this.get(pn)(p.defaultVal);
}
}
}, {
Expand Down
19 changes: 18 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ exports.deepEquals = deepEquals;
exports.extend = extend;
exports.identity = identity;
exports.isUndefined = isUndefined;
exports.isFunction = isFunction;
exports.isPlainObject = isPlainObject;
exports.isArray = isArray;
exports.mapKeys = mapKeys;
exports.merge = merge;

Expand Down Expand Up @@ -162,11 +165,13 @@ function extend(dest, src) {
}
} else if (isUndefined(src[prop])) {
dest[prop] = undefined;
} else if (src[prop].constructor === Object) {
} else if (isPlainObject(src[prop])) {
if (_shallow) {
dest[prop] = {};
}
extend(dest[prop], src[prop], createAsObservable);
} else if (isArray(src[prop])) {
dest[prop] = src[prop].slice(0);
} else {
dest[prop] = src[prop];
}
Expand Down Expand Up @@ -197,6 +202,18 @@ function isUndefined(x) {
return typeof x === 'undefined';
}

function isFunction(x) {
return typeof x === 'function';
}

function isPlainObject(x) {
return x.constructor === Object;
}

function isArray(x) {
return isFunction(x.splice);
}

function mapKeys(obj, fn) {
var mappedObj = {};
Object.keys(obj).forEach(function (k) {
Expand Down
1 change: 1 addition & 0 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class Context {
}

update(origUrl = this.canonicalPath(), state = false, push = true, query = false) {
// debugger
const url = this.resolveUrl(origUrl)
const route = this.getRouteForUrl(url)
const firstRun = this.route() === ''
Expand Down
17 changes: 14 additions & 3 deletions src/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ko from 'knockout'
import qs from 'qs'
import { deepEquals, identity, isUndefined, mapKeys, merge } from './utils'
import { deepEquals, extend, identity, isArray, isPlainObject, isUndefined, mapKeys, merge } from './utils'

const qsParams = {}
const trigger = ko.observable(true)
Expand Down Expand Up @@ -34,7 +34,6 @@ class Query {

if (!cache[guid][prop]) {
cache[guid][prop] = {
defaultVal,
parser,
value: ko.pureComputed({
read() {
Expand Down Expand Up @@ -67,6 +66,18 @@ class Query {
})
}
}

if (defaultVal) {
// clone to prevent defaultVal from being changed by reference
if (isArray(defaultVal)) {
cache[guid][prop].defaultVal = defaultVal.slice(0)
} else if (isPlainObject(defaultVal)) {
cache[guid][prop].defaultVal = extend({}, defaultVal, false)
} else {
cache[guid][prop].defaultVal = defaultVal
}
}

return cache[guid][prop].value
}

Expand Down Expand Up @@ -105,7 +116,7 @@ class Query {
const guid = this.ctx.config.depth + pathname
for (const pn in cache[guid]) {
const p = cache[guid][pn]
p.value(p.defaultVal)
this.get(pn)(p.defaultVal)
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ export function extend(dest, src, createAsObservable = true, _shallow = true) {
}
} else if (isUndefined(src[prop])) {
dest[prop] = undefined
} else if (src[prop].constructor === Object) {
} else if (isPlainObject(src[prop])) {
if (_shallow) {
dest[prop] = {}
}
extend(dest[prop], src[prop], createAsObservable)
} else if (isArray(src[prop])) {
dest[prop] = src[prop].slice(0)
} else {
dest[prop] = src[prop]
}
Expand All @@ -102,6 +104,18 @@ export function isUndefined(x) {
return typeof x === 'undefined'
}

export function isFunction(x) {
return typeof x === 'function'
}

export function isPlainObject(x) {
return x.constructor === Object
}

export function isArray(x) {
return isFunction(x.splice)
}

export function mapKeys(obj, fn) {
const mappedObj = {}
Object.keys(obj).forEach((k) => mappedObj[k] = fn(k))
Expand Down

0 comments on commit 6d18b10

Please sign in to comment.