/
index.js
33 lines (30 loc) · 887 Bytes
/
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
exports.register = function(name, tpl, vm) {
vm = vm || function() {};
switch (typeof vm) {
case 'function':
this.registerWithConstructor(name, tpl, vm);
return;
case 'object':
this.registerWithObject(name, tpl, vm);
return;
}
throw "Can't register KO component: \"" + name + "\", I don't know it's type!";
};
exports.registerWithConstructor = function(name, tpl, vm) {
ko.components.register(name, {
viewModel: vm,
template: tpl
});
};
exports.registerWithObject = function(name, tpl, vm) {
ko.components.register(name, {
viewModel: {
createViewModel: function(params, info) {
var i = Object.create(vm);
i.init && i.init(params, info);
return i;
}
},
template: tpl
});
};