forked from featurist/hyperdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.js
36 lines (29 loc) · 823 Bytes
/
binding.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
var meta = require('./meta')
module.exports = function (b) {
var binding = b
if (b instanceof Array) {
binding = bindingObject.apply(undefined, b)
} else if (b instanceof Object && (typeof b.set === 'function' || typeof b.get === 'function')) {
binding = b
} else {
throw Error('hyperdom bindings must be either an array [object, property, setter] or an object { get(), set(value) }, instead binding was: ' + JSON.stringify(b))
}
return binding
}
function bindingObject (model, property, setter) {
var _meta
return {
get: function () {
return model[property]
},
set: function (value) {
model[property] = value
if (setter) {
return setter(value)
}
},
meta: function () {
return _meta || (_meta = meta(model, property))
}
}
}