Thanks to visit codestin.com
Credit goes to github.com

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

Commit 801695e

Browse files
committed
IE Compatibility
1 parent d26ec0b commit 801695e

File tree

4 files changed

+45
-86
lines changed

4 files changed

+45
-86
lines changed

README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ import Query from 'ko-querystring'
2222
const query = new Query({ sort: 'alpha' })
2323

2424
query.sort() // alpha
25-
26-
// you can also create query parameters on the fly, e.g.
27-
query.foo('foo')
28-
// will create an observable on query.foo, and stick it in the querystring with
29-
// a value of "foo"
3025
```
3126

3227
## API
@@ -61,9 +56,6 @@ the querystring — but call to `query.clear()` will then set it to "foo". The
6156
function disallows setting the param to "baz", and attempting to will cause it
6257
to be set to "qux" instead.
6358

64-
Params to not have to be defined; attempting to access a query param not defined
65-
in the config will create one on-the-fly and it will be `undefined` until set.
66-
6759
__NOTE:__ Params that are equal to their default will _not_ be displayed in the
6860
querystring. Less === More.
6961

@@ -101,7 +93,7 @@ Resets param to its default or undefined.
10193
Observable value that is true if the param is its default value, otherwise false.
10294

10395
### query.setDefaults(defaults)
104-
Set or change the default values for a query.
96+
Change the default values for a query.
10597

10698
### query.clear()
10799
Reset all the query params to their defined defaults, or undefined.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ko-querystring",
3-
"version": "1.3.3",
3+
"version": "2.0.0",
44
"description": "Query(string) Abstraction for KnockoutJS",
55
"main": "dist/ko-querystring.js",
66
"files": [

src/index.js

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,17 @@ const links = {}
66

77
let _parse, _stringify
88

9-
function getCoercions(config) {
10-
const coercions = {}
11-
Object.entries(config).forEach(([k, v = {}]) => coercions[k] = v.coerce)
12-
return coercions
13-
}
14-
159
function getDefaults(config) {
1610
const defaults = {}
17-
Object.entries(config).forEach(([k, v = {}]) =>
18-
defaults[k] = v.default || v.initial || v.coerce
11+
Object.entries(config).forEach(([k, v]) =>
12+
defaults[k] = isQueryParamConfigObject(v)
1913
? v.default
2014
: v)
2115
return defaults
2216
}
2317

24-
function getInitialValues(config) {
25-
const inits = {}
26-
Object.entries(config).forEach(([k, v = {}]) =>
27-
inits[k] = v.default || v.initial || v.coerce
28-
? v.initial || v.default
29-
: v)
30-
return inits
18+
function isQueryParamConfigObject(c) {
19+
return c && (c.default || c.initial || c.coerce)
3120
}
3221

3322
class Query {
@@ -43,37 +32,34 @@ class Query {
4332
links[this._group]++
4433
}
4534

46-
const coercions = getCoercions(config)
47-
const initialValues = getInitialValues(config)
4835
const fromQS = Query.fromQS(this._group)
49-
const init = Object.assign({}, initialValues, fromQS)
5036

51-
const { proxy, revoke } = Proxy.revocable(this, {
52-
get: (_, name) => {
53-
if (name[0] === '_' || !isUndefined(this[name])) {
54-
return this[name]
55-
}
56-
if (isUndefined(query[this._group][name])) {
57-
query[this._group][name] = Query.createQuerySetterObservable(group, name, this._defaults[name], init[name], coercions[name])
58-
Object.assign(query[this._group][name], {
59-
isDefault: ko.pureComputed(() => query[this._group][name]() === this._defaults[name]),
60-
clear: () => {
61-
query[this._group][name](this._defaults[name])
37+
Object.entries(config).forEach(([name, config]) => {
38+
Object.defineProperty(this, name, {
39+
get: () => {
40+
const init = fromQS[name] || (config && config.initial) || this._defaults[name]
41+
const coerce = (config && config.coerce) || ((x) => x)
42+
43+
if (isUndefined(query[this._group][name])) {
44+
query[this._group][name] = Query.createQuerySetterObservable(group, name, this._defaults[name], init, coerce)
45+
Object.assign(query[this._group][name], {
46+
isDefault: ko.pureComputed(() => query[this._group][name]() === this._defaults[name]),
47+
clear: () => {
48+
query[this._group][name](this._defaults[name])
49+
}
50+
})
51+
if (this._forceRecompute) {
52+
ko.tasks.schedule(() => this._forceRecompute(!this._forceRecompute()))
6253
}
63-
})
64-
if (this._forceRecompute) {
65-
ko.tasks.schedule(() => this._forceRecompute(!this._forceRecompute()))
6654
}
55+
return query[this._group][name]
6756
}
68-
return query[this._group][name]
69-
}
70-
})
71-
72-
this.revoke = revoke
57+
})
7358

74-
Object.keys(init).forEach((k) => proxy[k])
59+
this[name]
60+
})
7561

76-
return proxy
62+
return this
7763
}
7864

7965
setDefaults(d) {
@@ -112,13 +98,12 @@ class Query {
11298

11399
dispose() {
114100
if (--links[this._group] === 0) {
115-
const current = Object.assign({}, Query.fromQS(), this.getCleanQuery())
101+
const current = Object.assign({}, Query.fromQS(), this.constructor.getCleanQuery())
116102
delete current[this._group]
117103
Query.writeQueryString(current)
118104

119105
delete query[this._group]
120106
}
121-
this.revoke()
122107
}
123108

124109
static get defaultParser() {
@@ -152,7 +137,7 @@ class Query {
152137

153138
static fromQS(group) {
154139
const query = this.parse(this.getQueryString())
155-
return isUndefined(group) ? query : query[group]
140+
return (isUndefined(group) ? query : query[group]) || {}
156141
}
157142

158143
static getCleanQuery() {

test.js

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,10 @@ ko.components.register('test', {
1919
t.end()
2020
})
2121

22-
test('implicit initialization', (t) => {
23-
history.replaceState(null, null, location.pathname + '?{"foo": "foo"}')
24-
25-
const query = new Query()
26-
27-
t.ok(ko.isWritableObservable(query.foo), 'can be created on-the-fly')
28-
t.equals('foo', query.foo(), 'implicit params are initialized from query string')
29-
30-
query.dispose()
31-
t.end()
32-
})
33-
3422
test('url parsing', (t) => {
3523
history.replaceState(null, null, location.pathname + '#hash')
3624

37-
const query = new Query()
25+
const query = new Query({ foo: undefined })
3826

3927
query.foo('foo')
4028
ko.tasks.runEarly()
@@ -48,7 +36,7 @@ ko.components.register('test', {
4836
test('empty query', (t) => {
4937
history.replaceState(null, null, location.pathname)
5038

51-
const query = new Query()
39+
const query = new Query({ foo: undefined })
5240

5341
query.foo('foo')
5442
query.foo(undefined)
@@ -183,7 +171,7 @@ ko.components.register('test', {
183171
test('#toJS', (t) => {
184172
history.replaceState(null, null, location.pathname + '?{"foo": "foo"}')
185173

186-
const query = new Query()
174+
const query = new Query({ foo: undefined })
187175

188176
t.deepEquals({ foo: 'foo' }, query.toJS(), 'returns unwrapped query object')
189177

@@ -211,7 +199,7 @@ ko.components.register('test', {
211199

212200
history.replaceState(null, null, location.pathname)
213201

214-
const query = new Query()
202+
const query = new Query({ foo: undefined })
215203
const q = query.asObservable()
216204

217205
const killMe = q.subscribe(() => {
@@ -250,8 +238,8 @@ ko.components.register('test', {
250238
test('#dispose', (t) => {
251239
history.replaceState(null, null, location.pathname)
252240

253-
const a1 = new Query({}, 'a')
254-
const a2 = new Query({}, 'a')
241+
const a1 = new Query({ foo: undefined }, 'a')
242+
const a2 = new Query({ foo: undefined }, 'a')
255243

256244
a1.foo('foo')
257245
ko.tasks.runEarly()
@@ -261,13 +249,6 @@ ko.components.register('test', {
261249

262250
t.deepEquals({ a: { foo: 'foo' } }, Query.parse(location.search.substring(1)), 'does not remove query if linked group remains')
263251

264-
try {
265-
a1.foo('notfoo')
266-
t.fail('does not dispose')
267-
} catch (e) {
268-
t.pass('disposes query')
269-
}
270-
271252
a2.dispose()
272253
ko.tasks.runEarly()
273254

@@ -277,8 +258,8 @@ ko.components.register('test', {
277258
})
278259

279260
test('grouped/multiple queries', (t) => {
280-
const a = new Query({}, 'a')
281-
const b = new Query({}, 'b')
261+
const a = new Query({ foo: undefined }, 'a')
262+
const b = new Query({ foo: undefined }, 'b')
282263

283264
a.foo('foo')
284265
b.foo('notfoo')
@@ -294,20 +275,21 @@ ko.components.register('test', {
294275
})
295276

296277
test('linked queries', (t) => {
297-
const a1 = new Query({}, 'a')
298-
a1.foo('foo')
278+
const a1 = new Query({ foo: undefined, bar: undefined }, 'a')
279+
const a2 = new Query({ foo: undefined, bar: 'bar', baz: undefined }, 'a')
299280

300-
const a2 = new Query({ bar: 'bar' }, 'a')
281+
a1.foo('foo')
301282

302283
ko.tasks.runEarly()
303284

304-
t.equals('foo', a2.foo(), 'links via setter')
305-
t.equals('bar', a1.bar(), 'links via defaults')
285+
t.equals('foo', a2.foo(), 'links')
306286

307287
a1.dispose()
308288

309-
const a3 = new Query({ baz: 'baz' }, 'a')
289+
const a3 = new Query({ baz: undefined }, 'a')
310290

291+
a3.baz('baz')
292+
311293
ko.tasks.runEarly()
312294

313295
t.equals('baz', a2.baz(), 'works after a linked query is disposed')
@@ -325,7 +307,7 @@ ko.components.register('test', {
325307
stringify: (obj) => 'foo=' + obj.foo
326308
})
327309

328-
const q = new Query()
310+
const q = new Query({ foo: undefined })
329311

330312
t.equals(q.foo(), 'foo', 'uses custom parse function')
331313

0 commit comments

Comments
 (0)