-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathko-ubp.js
59 lines (48 loc) · 2.16 KB
/
ko-ubp.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
/**
Virtual element support has been taken from knockout.
https://github.com/SteveSanderson/knockout/blob/master/src/virtualElements.js
*/
(function (ko, $) {
var bindings,
reBindingClassPrefix = /(?:^|\s)ko-/,
commentNodesHaveTextProperty = document.createComment("test").text === "<!--test-->",
startCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*ko\s+ko:\s+(.*)\s*-->$/ : /^\s*ko\s+ko:\s+(.*)\s*$/;
function virtualNodeBindingClasses(node) {
var match = (commentNodesHaveTextProperty ? node.text : node.nodeValue).match(startCommentRegex);
return match ? match[1] : null;
}
function hasBindings(node) {
switch (node.nodeType) {
case 1: return node.className && node.className.match && !!node.className.match(reBindingClassPrefix); // Element
case 8: return virtualNodeBindingClasses(node) !== null;
default: return false;
}
}
function getBindingsForElement($node, bindingContext) {
var nodeBindings = {};
$.each(bindings, function (selector, binding) {
if ($node.is(selector)) {
if ($.isFunction(binding)) binding = binding.call(bindingContext.$data, bindingContext.$parent, bindingContext.$parents, bindingContext.$index);
nodeBindings = $.extend(nodeBindings, binding);
}
});
return nodeBindings;
}
function getBindingsForComment(bindingClasses, bindingContext) {
var $worker = $('<div />').addClass(bindingClasses);
return getBindingsForElement($worker, bindingContext);
}
function getBindings(node, bindingContext) {
switch (node.nodeType) {
case 1: return getBindingsForElement($(node), bindingContext);
case 8: return getBindingsForComment(virtualNodeBindingClasses(node), bindingContext);
default: return null;
}
}
function setBindings(b) { bindings = b; }
ko.bindingProvider.instance = {
nodeHasBindings: hasBindings,
getBindings: getBindings,
setBindings: setBindings
};
})(ko, jQuery);