-
Notifications
You must be signed in to change notification settings - Fork 3
/
iron_query.js
78 lines (61 loc) · 1.46 KB
/
iron_query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var path, pathname, queryObj, query = {};
query.keyDeps = {};
query.ensureKey = function (key) {
var deps = this.keyDeps;
if (!(key in deps))
deps[key] = new Tracker.Dependency;
deps[key].depend();
};
query.get = function (key) {
if (key) {
this.ensureKey(key);
return path.queryObject[key];
} else {
if (!this.dep)
this.dep = new Tracker.Dependency;
this.dep.depend();
return path.queryObject;
}
};
query.set = function (key, val) {
this.wait(key, val);
this.go();
};
query.getNonreactive = function (key) {
return key ? queryObj[key]: queryObj;
};
query.wait = function (key, val) {
var oldVal = queryObj[key];
if (String(val) === oldVal || !oldVal && !val)
return;
if (typeof val != 'undefined' && val != null && val != '')
queryObj[key] = String(val);
else
delete queryObj[key];
var dep = this.keyDeps[key];
dep && dep.changed();
this.dep && this.dep.changed();
};
query.go = function () {
var query = Iron.Url.toQueryString(queryObj);
Iron.Location.go(pathname + query + path.hash);
};
Iron.query = query;
Tracker.autorun(function () {
path = Iron.Location.get();
if (pathname) {
var deps = query.keyDeps;
for (var key in deps) {
deps[key].changed();
};
query.dep && query.dep.changed();
}
pathname = path.pathname;
queryObj = path.queryObject;
});
UI.registerHelper('ironQuery', function (key) {
return query.getNonreactive(key);
});
UI.registerHelper('ironQueryReactive', function (key) {
return query.get(key);
});