KnockoutJS to ES2015/ES6

TODO: use the new Proxy for ko.observable
This commit is contained in:
djmaze 2020-10-04 21:58:13 +02:00
parent a0f8ac0dad
commit 72ed2b08b2
56 changed files with 941 additions and 1873 deletions

View file

@ -3,18 +3,18 @@ ko.utils.domData = new (function () {
var uniqueId = 0;
var dataStoreKeyExpandoPropertyName = "__ko__" + (Date.now());
var getDataForNode, clear;
var
// We considered using WeakMap, but it has a problem in IE 11 and Edge that prevents using
// it cross-window, so instead we just store the data directly on the node.
// See https://github.com/knockout/knockout/issues/2141
getDataForNode = function (node, createIfNotFound) {
getDataForNode = (node, createIfNotFound) => {
var dataForNode = node[dataStoreKeyExpandoPropertyName];
if (!dataForNode && createIfNotFound) {
dataForNode = node[dataStoreKeyExpandoPropertyName] = {};
}
return dataForNode;
};
clear = function (node) {
},
clear = node => {
if (node[dataStoreKeyExpandoPropertyName]) {
delete node[dataStoreKeyExpandoPropertyName];
return true; // Exposing "did clean" flag purely so specs can infer whether things have been cleaned up as intended
@ -23,24 +23,22 @@ ko.utils.domData = new (function () {
};
return {
get: function (node, key) {
get: (node, key) => {
var dataForNode = getDataForNode(node, false);
return dataForNode && dataForNode[key];
},
set: function (node, key, value) {
set: (node, key, value) => {
// Make sure we don't actually create a new domData key if we are actually deleting a value
var dataForNode = getDataForNode(node, value !== undefined /* createIfNotFound */);
dataForNode && (dataForNode[key] = value);
},
getOrSet: function (node, key, value) {
getOrSet: (node, key, value) => {
var dataForNode = getDataForNode(node, true /* createIfNotFound */);
return dataForNode[key] || (dataForNode[key] = value);
},
clear: clear,
nextKey: function () {
return (uniqueId++) + dataStoreKeyExpandoPropertyName;
}
nextKey: () => (uniqueId++) + dataStoreKeyExpandoPropertyName
};
})();