Commit 4cc5d7c35493700dc9a40f3cb6da6a2b6e3ce7dc
when: resolve inner observs if root is not observable
Matt McKegg committed on 7/15/2016, 4:42:04 AMParent: 1964140bd2d8f29be42fb1ff7af768d7fb89ef59
Files changed
when.js | changed |
when.js | ||
---|---|---|
@@ -1,11 +1,29 @@ | ||
1 | 1 … | var computed = require('./computed') |
2 … | +var isObservable = require('./is-observable') | |
2 | 3 … | |
3 | 4 … | module.exports = When |
4 | 5 … | |
5 | 6 … | function When (obs, ifTrue, ifFalse) { |
7 … | + ifTrue = handleInnerValues(ifTrue) | |
8 … | + ifFalse = handleInnerValues(ifFalse) | |
6 | 9 … | return computed([obs, ifTrue, ifFalse], lambda) |
7 | 10 … | } |
8 | 11 … | |
9 | 12 … | function lambda (value, ifTrue, ifFalse) { |
10 | 13 … | return value ? ifTrue : ifFalse |
11 | 14 … | } |
15 … | + | |
16 … | +function handleInnerValues (obs) { | |
17 … | + if (Array.isArray(obs) && obs.some(isObservable)) { | |
18 … | + // HACK: resolve inner observs | |
19 … | + var inner = [] | |
20 … | + return computed(obs, function () { | |
21 … | + for (var i = 0; i < arguments.length; i++) { | |
22 … | + inner[i] = arguments[i] | |
23 … | + } | |
24 … | + inner.length = arguments.length | |
25 … | + return inner | |
26 … | + }) | |
27 … | + } | |
28 … | + return obs | |
29 … | +} |
Built with git-ssb-web