-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombined.coffee
83 lines (79 loc) · 2.59 KB
/
combined.coffee
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
79
80
81
82
83
{identity,noop} = require "./_helpers"
module.exports =
_name: "combined"
_v: 1
mixins: [
require("./watch")
require("./computed")
]
methods:
$combined: (o) ->
@$path.toNameAndParent(o)
unless o.parent.hasOwnProperty(o.name)
Object.defineProperty o.parent, o.name, __proto__:null, value: {}
combinedParent = o.parent[o.name]
Object.keys(o.value).forEach (name) =>
v = o.value[name]
makeHiddenName = (name2) -> "__#{name}_#{name2}"
createObj = (obj) ->
obj.path = "#{o.path}.#{obj.name}"
obj.parent = combinedParent
return obj
if v.computed?
computed = makeHiddenName("computed")
@$computed.init createObj
name: computed
get: v.computed
else
computed = null
if v.data?
data = name
@$watch.path createObj
name: data
value: v.data.call(@)
else
data = null
combined = makeHiddenName("combined")
deferred = ->
getterFactory = (parent, combined, prop, computed, data, normalize, parseProp) ->
getter = ->
if prop and (propVal = @[prop])?
obj = normalize(parseProp(propVal))
else
obj = {}
if computed
for k,v of normalize(parent[computed])
obj[k] ?= v
if data
for k,v of normalize(parent[data])
obj[k] ?= v
return obj
return getter.bind(@)
@$computed.init createObj
name: combined
cbs: o.cbFactory.call(@,name)
get: getterFactory.call(@, combinedParent, combined, v.prop, computed, data, o.normalize || identity, o.parseProp || identity)
if @$computed.__deferredInits
Object.defineProperty combinedParent, combined, configurable: true, set: noop, get: noop
@$computed.__deferredInits.push deferred
else
deferred.call(@)
test module.exports, {
data: -> someProp: parentProp: "parentProp1"
combinedTest: {}
}, (el) ->
it "should work", (done) ->
el.combinedTest =
name:
data: -> parentData: "parentData1"
computed: -> parentComputed: "parentComputed1"
prop: "someProp"
el.$combined
path: "combinedTest"
value: el.combinedTest
cbFactory: -> [(val) ->
val.parentData.should.equal "parentData1"
val.parentComputed.should.equal "parentComputed1"
val.parentProp.should.equal "parentProp1"
done()
]