-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (63 loc) · 2.02 KB
/
index.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
79
window.Bindable = (function() {
'use strict';
function Bindable(context, dataKey) {
context = document[context] || document
this.dataKey = dataKey || 'data-bindable'
this.instanceKey = this.dataKey.replace(/data-/g, '') + 'Instance'
this.bindables = context.querySelectorAll('[' + this.dataKey + ']')
}
Bindable.prototype.bindAll = function() {
for (var i = 0, len = this.bindables.length; i < len; i += 1) {
this.bind(this.bindables[i])
}
return this
};
Bindable.prototype.getRefs = function() {
var refs = []
for (var i = 0, len = this.bindables.length; i < len; i += 1) {
refs.push(this.bindables[i][this.instanceKey])
}
return refs
};
Bindable.prototype.dispose = function() {
var instance, bindable;
for (var i = 0, len = this.bindables.length; i < len; i += 1) {
bindable = this.bindables[i]
if (instance = bindable[this.instanceKey]) {
if (typeof (instance != null ? instance.dispose : void 0) === 'function') {
instance.dispose()
}
bindable[this.instanceKey] = null
}
}
this.bindables = []
return this
};
Bindable.prototype.bind = function(el, dataKey) {
var _class, key;
dataKey = dataKey || this.dataKey
key = el.getAttribute(dataKey)
if (_class = this.constructor.getClass(key)) {
if (!el[this.instanceKey]) {
el[this.instanceKey] = new _class(el)
}
return el[this.instanceKey]
} else {
if (typeof console !== "undefined" && console !== null) {
console.error('Bindable for key: ' + key + ' not found in Bindable.registry for instance ' + el)
}
return void 0
}
};
Bindable.getClass = function(key) {
var _key = "" + key
return (this.registry[_key] ? this.registry[_key]['class'] : void 0)
};
Bindable.register = function(key, klass) {
var _key = "" + key
this.registry = this.registry || {}
this.registry[_key] = {'class': klass}
return this.registry
};
return Bindable
})();