-
Notifications
You must be signed in to change notification settings - Fork 2
/
knockout.component.js
197 lines (166 loc) · 8.09 KB
/
knockout.component.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright: Denys Khanzhiyev
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
*/
!function($){
!function(factory){
// Support three module loading scenarios
// stolen from knockoutjs source
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// [1] CommonJS/Node.js
var target = module['exports'] || exports; // module.exports is for Node.js
factory(target);
} else if (typeof define === 'function' && define['amd']) {
// [2] AMD anonymous module
define(['exports'], factory);
} else {
// [3] No module loader (plain <script> tag) - put directly in global namespace
factory(window['kc'] = {});
}
}(function(kcExports){
var
kc = typeof kcExports !== 'undefined' ? kcExports : {};
components = {};
function bindComponentProperty(name,instance,binding,parentContext) {
var
blockMine = false, blockOther = false, bindToObj, subscriptions=[];
if (ko.isSubscribable(instance[name])){
if (ko.isWriteableObservable(binding) ) subscriptions.push(instance[name].subscribe(function (val) {
if (blockMine) return;
blockMine = true;
binding(val);
blockMine = false;
}));
} else instance[name] = ko.utils.unwrapObservable(binding);
if (ko.isSubscribable(binding)){
if( ko.isWriteableObservable(instance[name])){
subscriptions.push(binding.subscribe(function (val) {
if (blockOther) return;
blockOther = true;
instance[name](val);
blockOther = false;
}));
blockOther = true;
instance[name](binding());
blockOther = false;
}
else subscriptions.push(binding.subscribe(function (val) {
instance[name] = val;
}))
}
else if(ko.isWriteableObservable(instance[name])) instance[name](binding);
return subscriptions;
}
var componentVMInstanceDomDataKey = '__kc__componentVMInstanceDomDataKey__';
var templateSubscriptionDomDataKey = '__kc__templateSubscriptionDomDataKey__';
function disposeOldSubscriptionAndStoreNewOne(element, newSubscription) {
var oldSubscription = ko.utils.domData.get(element, templateSubscriptionDomDataKey);
if (oldSubscription && (typeof(oldSubscription.dispose) == 'function'))
oldSubscription.dispose();
ko.utils.domData.set(element, templateSubscriptionDomDataKey, newSubscription);
}
ko.bindingHandlers.component = {
init:function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
var val = valueAccessor(), name;
valueUnwrapped = ko.utils.unwrapObservable(val);
if(typeof valueUnwrapped == "string") {
name = valueUnwrapped;
}
else throw new Error('component binding value must be component name');
if(!components[name]) throw new Error('component not found '+name);
var
bindings = allBindingsAccessor(),
componentBinding = bindings['componentBinding'],
componentOptions = bindings['componentOptions']||bindings['kc.options'],
componentAssignTo = bindings['componentAssignTo']||bindings['kc.assign'],
subscriptions=[],
oldInstance = ko.utils.domData.get(element, componentVMInstanceDomDataKey),
newInstance = components[name].factory(componentOptions,element);
// if(oldInstance) make something with it
ko.utils.domData.set(element, componentVMInstanceDomDataKey,newInstance);
if(typeof componentAssignTo == 'function') componentAssignTo(newInstance);
if(componentBinding && (typeof componentBinding == 'object')){
for(var prop in componentBinding) {
if(componentBinding.hasOwnProperty(prop)) {
subscriptions = subscriptions.concat(bindComponentProperty(prop,newInstance,componentBinding[prop],bindingContext));
}
}
if(subscriptions.length) ko.utils.domNodeDisposal.addDisposeCallback(element,function(){
ko.utils.arrayForEach(subscriptions,function(s){s.dispose();});
});
}
return { controlsDescendantBindings: true };
},
update:function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
var
templateSubscription,
componentVM = ko.utils.domData.get(element, componentVMInstanceDomDataKey),
childBindingContext = bindingContext.createChildContext(componentVM);
var val = valueAccessor(), name,
bindings = allBindingsAccessor();
valueUnwrapped = ko.utils.unwrapObservable(val);
if(typeof valueUnwrapped == "string") {
name = valueUnwrapped;
}
else throw new Error('component binding value must be component name');
if(!components[name]) throw new Error('component not found '+name);
if(components[name].templateReady) components[name].templateReady(function(template){
if (template) {
templateSubscription = ko.renderTemplate(template, childBindingContext, bindings.templateOptions||{}, element);
disposeOldSubscriptionAndStoreNewOne(element, templateSubscription);
}
});
}
}
ko.virtualElements.allowedBindings['component'] = true;
function Component(name,factory,template)
{
components[name] = this;
this.factory = factory;
this.template = template;
if( (typeof template == 'string') || (typeof template == 'function')){
this.templateReady = function(cb){
cb(template);
}
} else if(template && template.url && $){
var tname = 'kc.'+name,
_promise = $.get(url,function(data){
$('body').append('<script id="'+tname+'" type="text/html">'+data+'</script>');
});
this.templateReady = function(cb){
_promise.then(function(){ cb(tname); });
};
} else if(template && template.templateReady){
this.templateReady = template.templateReady;
}
ko.bindingHandlers['kc.'+name] = {
init:function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
var val = valueAccessor(),
valueUnwrapped = ko.utils.unwrapObservable(val),
bindings = allBindingsAccessor();
bindings.componentBinding = valueUnwrapped;
return ko.bindingHandlers['component'].init(element,function(){return name;},function(){return bindings;},viewModel,bindingContext);
},
update:function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
var val = valueAccessor(),
valueUnwrapped = ko.utils.unwrapObservable(val),
bindings = allBindingsAccessor();
bindings.componentBinding = valueUnwrapped;
return ko.bindingHandlers['component'].update(element,function(){return name;},function(){return bindings;},viewModel,bindingContext);
}
}
ko.virtualElements.allowedBindings['kc.'+name] = true;
}
kc.component = function(name,template,viewModelFactory){
var obj = name;
if(name && (typeof name == 'object')){
name = obj.name;
template = obj.template;
viewModelFactory = obj.create;
}
return new Component(name,viewModelFactory,template);
};
// kc.components = components;
return kc;
});
}(window["jQuery"]);