Remove some unused KnockoutJS features

This commit is contained in:
djmaze 2021-09-13 16:20:29 +02:00
parent f09820d0b9
commit 5839bcf62d
7 changed files with 100 additions and 184 deletions

View file

@ -113,15 +113,6 @@
extendCallback = options['extend'];
}
if (dataItemAlias && options && options['noChildContext']) {
var isFunc = typeof(dataItemOrAccessor) == "function" && !ko.isObservable(dataItemOrAccessor);
return new ko.bindingContext(inheritParentVm, this, null, self => {
if (extendCallback)
extendCallback(self);
self[dataItemAlias] = isFunc ? dataItemOrAccessor() : dataItemOrAccessor;
}, options);
}
return new ko.bindingContext(dataItemOrAccessor, this, dataItemAlias, (self, parentContext) => {
// Extend the context hierarchy by setting the appropriate pointers
self['$parentContext'] = parentContext;

View file

@ -18,12 +18,7 @@ ko.bindingHandlers['foreach'] = {
return {
'foreach': unwrappedValue['data'],
'as': unwrappedValue['as'],
'noChildContext': unwrappedValue['noChildContext'],
'includeDestroyed': unwrappedValue['includeDestroyed'],
'afterAdd': unwrappedValue['afterAdd'],
'beforeRemove': unwrappedValue['beforeRemove'],
'afterRender': unwrappedValue['afterRender'],
'beforeMove': unwrappedValue['beforeMove'],
'afterMove': unwrappedValue['afterMove']
};
};

View file

@ -4,12 +4,11 @@
function makeWithIfBinding(bindingKey, isWith, isNot) {
ko.bindingHandlers[bindingKey] = {
'init': (element, valueAccessor, allBindings, viewModel, bindingContext) => {
var didDisplayOnLastUpdate, savedNodes, contextOptions = {}, needAsyncContext, renderOnEveryChange;
var didDisplayOnLastUpdate, savedNodes, contextOptions = {}, needAsyncContext;
if (isWith) {
var as = allBindings.get('as'), noChildContext = allBindings.get('noChildContext');
renderOnEveryChange = !(as && noChildContext);
contextOptions = { 'as': as, 'noChildContext': noChildContext, 'exportDependencies': renderOnEveryChange };
var as = allBindings.get('as'), noChildContext = false;
contextOptions = { 'as': as, 'exportDependencies': true };
}
needAsyncContext = allBindings['has'](ko.bindingEvent.descendantsComplete);
@ -20,18 +19,12 @@ function makeWithIfBinding(bindingKey, isWith, isNot) {
isInitial = !savedNodes,
childContext;
if (!renderOnEveryChange && shouldDisplay === didDisplayOnLastUpdate) {
return;
}
if (needAsyncContext) {
bindingContext = ko.bindingEvent.startPossiblyAsyncContentBinding(element, bindingContext);
}
if (shouldDisplay) {
if (!isWith || renderOnEveryChange) {
contextOptions['dataDependency'] = ko.dependencyDetection.computed();
}
contextOptions['dataDependency'] = ko.dependencyDetection.computed();
if (isWith) {
childContext = bindingContext['createChildContext'](typeof value == "function" ? value : valueAccessor, contextOptions);

View file

@ -60,24 +60,18 @@
var nodesToDelete = [];
var itemsToMoveFirstIndexes = [];
var itemsForBeforeRemoveCallbacks = [];
var itemsForBeforeMoveCallbacks = [];
var itemsForAfterMoveCallbacks = [];
var itemsForAfterAddCallbacks = [];
var mapData;
var countWaitingForRemove = 0;
function itemAdded(value) {
mapData = { arrayEntry: value, indexObservable: ko.observable(currentArrayIndex++) };
newMappingResult.push(mapData);
if (!isFirstExecution) {
itemsForAfterAddCallbacks[currentArrayIndex - 1] = mapData;
}
}
function itemMovedOrRetained(oldPosition) {
mapData = lastMappingResult[oldPosition];
if (currentArrayIndex !== mapData.indexObservable.peek()) {
itemsForBeforeMoveCallbacks[mapData.indexObservable.peek()] = mapData;
itemsForAfterMoveCallbacks[currentArrayIndex] = mapData;
}
// Since updating the index might change the nodes, do so before calling fixUpContinuousNodeArray
@ -169,9 +163,6 @@
// Store a copy of the array items we just considered so we can difference it next time
ko.utils.domData.set(domNode, lastMappingResultDomDataKey, newMappingResult);
// Call beforeMove first before any changes have been made to the DOM
callCallback(options['beforeMove'], itemsForBeforeMoveCallbacks);
// Next remove nodes for deleted items (or just clean if there's a beforeRemove callback)
nodesToDelete.forEach(options['beforeRemove'] ? ko.cleanNode : ko.removeNode);
@ -239,6 +230,5 @@
// Finally call afterMove and afterAdd callbacks
callCallback(options['afterMove'], itemsForAfterMoveCallbacks);
callCallback(options['afterAdd'], itemsForAfterAddCallbacks);
}
})();