Skip to content

Commit

Permalink
Replace the static "key" strings used for ko.utils.domData with dynam…
Browse files Browse the repository at this point in the history
…ically generated strings. This saves 385 bytes (83 gzipped).
  • Loading branch information
mbest committed Aug 31, 2013
1 parent 44fd8a0 commit 5c88580
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/binding/bindingAttributeSyntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@
}
}

var boundElementDomDataKey = '__ko_boundElement';
var boundElementDomDataKey = ko.utils.domData.nextKey();


function topologicalSortBindings(bindings) {
// Depth-first sort
Expand Down Expand Up @@ -381,7 +382,7 @@
};
};

var storedBindingContextDomDataKey = "__ko_bindingContext__";
var storedBindingContextDomDataKey = ko.utils.domData.nextKey();
ko.storedBindingContextForNode = function (node, bindingContext) {
if (arguments.length == 2) {
ko.utils.domData.set(node, storedBindingContextDomDataKey, bindingContext);
Expand Down
4 changes: 2 additions & 2 deletions src/binding/defaultBindings/ifIfnotWith.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var withIfDomDataKey = '__ko_withIfBindingData';
var withIfDomDataKey = ko.utils.domData.nextKey();
// Makes a binding like with or if
function makeWithIfBinding(bindingKey, isWith, isNot, makeContextCallback) {
ko.bindingHandlers[bindingKey] = {
'init': function(element, valueAccessor, allBindings, viewModel, bindingContext) {
'init': function(element) {
ko.utils.domData.set(element, withIfDomDataKey, {});
return { 'controlsDescendantBindings': true };
},
Expand Down
2 changes: 1 addition & 1 deletion src/binding/defaultBindings/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ ko.bindingHandlers['options'] = {
element.scrollTop = previousScrollTop;
}
};
ko.bindingHandlers['options'].optionValueDomDataKey = '__ko.optionValueDomData__';
ko.bindingHandlers['options'].optionValueDomDataKey = ko.utils.domData.nextKey();
2 changes: 1 addition & 1 deletion src/binding/editDetection/arrayToDomNodeChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
return { mappedNodes : mappedNodes, dependentObservable : (dependentObservable.isActive() ? dependentObservable : undefined) };
}

var lastMappingResultDomDataKey = "setDomNodeChildrenFromArrayMapping_lastMappingResult";
var lastMappingResultDomDataKey = ko.utils.domData.nextKey();

ko.utils.setDomNodeChildrenFromArrayMapping = function (domNode, array, mapping, options, callbackAfterAddingNodes) {
// Compare the provided array against the previous one
Expand Down
7 changes: 4 additions & 3 deletions src/templating/templateSources.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
}
};

var dataDomDataPrefix = ko.utils.domData.nextKey() + "_";
ko.templateSources.domElement.prototype['data'] = function(key /*, valueToWrite */) {
if (arguments.length === 1) {
return ko.utils.domData.get(this.domElement, "templateSourceData_" + key);
return ko.utils.domData.get(this.domElement, dataDomDataPrefix + key);
} else {
ko.utils.domData.set(this.domElement, "templateSourceData_" + key, arguments[1]);
ko.utils.domData.set(this.domElement, dataDomDataPrefix + key, arguments[1]);
}
};

Expand All @@ -61,7 +62,7 @@
// For compatibility, you can also read "text"; it will be serialized from the nodes on demand.
// Writing to "text" is still supported, but then the template data will not be available as DOM nodes.

var anonymousTemplatesDomDataKey = "__ko_anon_template__";
var anonymousTemplatesDomDataKey = ko.utils.domData.nextKey();
ko.templateSources.anonymousTemplate = function(element) {
this.domElement = element;
}
Expand Down
2 changes: 1 addition & 1 deletion src/templating/templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
}, null, { disposeWhenNodeIsRemoved: targetNode });
};

var templateComputedDomDataKey = '__ko__templateComputedDomDataKey__';
var templateComputedDomDataKey = ko.utils.domData.nextKey();
function disposeOldComputedAndStoreNewOne(element, newComputed) {
var oldComputed = ko.utils.domData.get(element, templateComputedDomDataKey);
if (oldComputed && (typeof(oldComputed.dispose) == 'function'))
Expand Down
36 changes: 21 additions & 15 deletions src/utils.domData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@ ko.utils.domData = new (function () {
var uniqueId = 0;
var dataStoreKeyExpandoPropertyName = "__ko__" + (new Date).getTime();
var dataStore = {};

function getAll(node, createIfNotFound) {
var dataStoreKey = node[dataStoreKeyExpandoPropertyName];
var hasExistingDataStore = dataStoreKey && (dataStoreKey !== "null") && dataStore[dataStoreKey];
if (!hasExistingDataStore) {
if (!createIfNotFound)
return undefined;
dataStoreKey = node[dataStoreKeyExpandoPropertyName] = "ko" + uniqueId++;
dataStore[dataStoreKey] = {};
}
return dataStore[dataStoreKey];
}

return {
get: function (node, key) {
var allDataForNode = ko.utils.domData.getAll(node, false);
var allDataForNode = getAll(node, false);
return allDataForNode === undefined ? undefined : allDataForNode[key];
},
set: function (node, key, value) {
if (value === undefined) {
// Make sure we don't actually create a new domData key if we are actually deleting a value
if (ko.utils.domData.getAll(node, false) === undefined)
if (getAll(node, false) === undefined)
return;
}
var allDataForNode = ko.utils.domData.getAll(node, true);
var allDataForNode = getAll(node, true);
allDataForNode[key] = value;
},
getAll: function (node, createIfNotFound) {
var dataStoreKey = node[dataStoreKeyExpandoPropertyName];
var hasExistingDataStore = dataStoreKey && (dataStoreKey !== "null") && dataStore[dataStoreKey];
if (!hasExistingDataStore) {
if (!createIfNotFound)
return undefined;
dataStoreKey = node[dataStoreKeyExpandoPropertyName] = "ko" + uniqueId++;
dataStore[dataStoreKey] = {};
}
return dataStore[dataStoreKey];
},
clear: function (node) {
var dataStoreKey = node[dataStoreKeyExpandoPropertyName];
if (dataStoreKey) {
Expand All @@ -36,8 +38,12 @@ ko.utils.domData = new (function () {
return true; // Exposing "did clean" flag purely so specs can infer whether things have been cleaned up as intended
}
return false;
},

nextKey: function () {
return (uniqueId++) + dataStoreKeyExpandoPropertyName;
}
}
};
})();

ko.exportSymbol('utils.domData', ko.utils.domData);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.domNodeDisposal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

ko.utils.domNodeDisposal = new (function () {
var domDataKey = "__ko_domNodeDisposal__" + (new Date).getTime();
var domDataKey = ko.utils.domData.nextKey();
var cleanableNodeTypes = { 1: true, 8: true, 9: true }; // Element, Comment, Document
var cleanableNodeTypesWithDescendants = { 1: true, 9: true }; // Element, Document

Expand Down

0 comments on commit 5c88580

Please sign in to comment.