KnockoutJS disposeWhenNodeIsRemoved not used external

This commit is contained in:
djmaze 2021-10-04 13:47:47 +02:00
parent c9ebb5a767
commit 4fff41580e
4 changed files with 52 additions and 53 deletions

View file

@ -44,17 +44,17 @@
clearCachedDefinition: componentName => clearCachedDefinition: componentName =>
delete loadedDefinitionsCache[componentName], delete loadedDefinitionsCache[componentName],
register: (componentName, config) => { register: (componentName, config) => {
if (!config) { if (!config) {
throw new Error('Invalid configuration for ' + componentName); throw new Error('Invalid configuration for ' + componentName);
} }
if (defaultConfigRegistry[componentName]) { if (defaultConfigRegistry[componentName]) {
throw new Error('Component ' + componentName + ' is already registered'); throw new Error('Component ' + componentName + ' is already registered');
} }
defaultConfigRegistry[componentName] = config; defaultConfigRegistry[componentName] = config;
} }
}; };
// The default loader is responsible for two things: // The default loader is responsible for two things:
@ -70,49 +70,49 @@
var defaultConfigRegistry = Object.create(null); var defaultConfigRegistry = Object.create(null);
var createViewModelKey = 'createViewModel'; var createViewModelKey = 'createViewModel';
// Takes a config object of the form { template: ..., viewModel: ... }, and asynchronously convert it // Takes a config object of the form { template: ..., viewModel: ... }, and asynchronously convert it
// into the standard component definition format: // into the standard component definition format:
// { template: <ArrayOfDomNodes>, createViewModel: function(params, componentInfo) { ... } }. // { template: <ArrayOfDomNodes>, createViewModel: function(params, componentInfo) { ... } }.
// Since both template and viewModel may need to be resolved asynchronously, both tasks are performed // Since both template and viewModel may need to be resolved asynchronously, both tasks are performed
// in parallel, and the results joined when both are ready. We don't depend on any promises infrastructure, // in parallel, and the results joined when both are ready. We don't depend on any promises infrastructure,
// so this is implemented manually below. // so this is implemented manually below.
function loadComponent(componentName, callback) { function loadComponent(componentName, callback) {
var result = {}, var result = {},
config = defaultConfigRegistry[componentName] || {}, config = defaultConfigRegistry[componentName] || {},
templateConfig = config['template'], templateConfig = config['template'],
viewModelConfig = config['viewModel']; viewModelConfig = config['viewModel'];
if (templateConfig) { if (templateConfig) {
if (!templateConfig['element']) { if (!templateConfig['element']) {
throwError(componentName, 'Unknown template value: ' + templateConfig); throwError(componentName, 'Unknown template value: ' + templateConfig);
} }
// Element ID - find it, then copy its child nodes // Element ID - find it, then copy its child nodes
var element = templateConfig['element']; var element = templateConfig['element'];
var elemInstance = document.getElementById(element); var elemInstance = document.getElementById(element);
if (!elemInstance) { if (!elemInstance) {
throwError(componentName, 'Cannot find element with ID ' + element); throwError(componentName, 'Cannot find element with ID ' + element);
} }
if (!elemInstance.matches('TEMPLATE')) { if (!elemInstance.matches('TEMPLATE')) {
throwError(componentName, 'Template Source Element not a <template>'); throwError(componentName, 'Template Source Element not a <template>');
} }
// For browsers with proper <template> element support (i.e., where the .content property // For browsers with proper <template> element support (i.e., where the .content property
// gives a document fragment), use that document fragment. // gives a document fragment), use that document fragment.
result['template'] = ko.utils.cloneNodes(elemInstance.content.childNodes); result['template'] = ko.utils.cloneNodes(elemInstance.content.childNodes);
} }
if (viewModelConfig) { if (viewModelConfig) {
if (typeof viewModelConfig[createViewModelKey] !== 'function') { if (typeof viewModelConfig[createViewModelKey] !== 'function') {
throwError(componentName, 'Unknown viewModel value: ' + viewModelConfig); throwError(componentName, 'Unknown viewModel value: ' + viewModelConfig);
} }
// Already a factory function - use it as-is // Already a factory function - use it as-is
result[createViewModelKey] = viewModelConfig[createViewModelKey]; result[createViewModelKey] = viewModelConfig[createViewModelKey];
} }
if (result['template'] && result[createViewModelKey]) { if (result['template'] && result[createViewModelKey]) {
callback(result); callback(result);
} else { } else {
callback(null); callback(null);
} }
} }
function throwError(componentName, message) { function throwError(componentName, message) {

View file

@ -4,10 +4,10 @@ ko.exportSymbol = (koPath, object) => {
// In the future, "ko" may become distinct from "koExports" (so that non-exported objects are not reachable) // In the future, "ko" may become distinct from "koExports" (so that non-exported objects are not reachable)
// At that point, "target" would be set to: (typeof koExports !== "undefined" ? koExports : ko) // At that point, "target" would be set to: (typeof koExports !== "undefined" ? koExports : ko)
var target = ko; var target = ko, i = 0, l = tokens.length - 1;
for (var i = 0; i < tokens.length - 1; i++) for (; i < l; i++)
target = target[tokens[i]]; target = target[tokens[i]];
target[tokens[tokens.length - 1]] = object; target[tokens[l]] = object;
}; };
ko.exportProperty = (owner, publicName, object) => owner[publicName] = object; ko.exportProperty = (owner, publicName, object) => owner[publicName] = object;

View file

@ -9,7 +9,6 @@ class koSubscription
this._node = null; this._node = null;
this._domNodeDisposalCallback = null; this._domNodeDisposalCallback = null;
ko.exportProperty(this, 'dispose', this.dispose); ko.exportProperty(this, 'dispose', this.dispose);
ko.exportProperty(this, 'disposeWhenNodeIsRemoved', this.disposeWhenNodeIsRemoved);
} }
dispose() { dispose() {

View file

@ -77,7 +77,7 @@ ko.utils = {
string.toString().replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''), string.toString().replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''),
domNodeIsAttachedToDocument: node => domNodeIsAttachedToDocument: node =>
node.ownerDocument.documentElement.contains(node.nodeType !== 1 ? node.parentNode : node), node.ownerDocument.documentElement.contains(node.nodeType !== 1 ? node.parentNode : node),
triggerEvent: (element, eventType) => { triggerEvent: (element, eventType) => {
if (!(element && element.nodeType)) if (!(element && element.nodeType))