Bugfix: Knockout 'with:' used on 1 place

This commit is contained in:
djmaze 2020-10-13 10:15:50 +02:00
parent 0eba94f671
commit e7975d4f82
4 changed files with 103 additions and 86 deletions

View file

@ -1,11 +1,17 @@
(() => {
(function () {
// Makes a binding like with or if
function makeIfBinding(bindingKey, isNot) {
function makeWithIfBinding(bindingKey, isWith, isNot) {
ko.bindingHandlers[bindingKey] = {
'init': (element, valueAccessor, allBindings, viewModel, bindingContext) => {
var didDisplayOnLastUpdate, savedNodes, contextOptions = {}, completeOnRender, needAsyncContext, renderOnEveryChange;
if (isWith) {
var as = allBindings.get('as'), noChildContext = allBindings.get('noChildContext');
renderOnEveryChange = !(as && noChildContext);
contextOptions = { 'as': as, 'noChildContext': noChildContext, 'exportDependencies': renderOnEveryChange };
}
completeOnRender = allBindings.get("completeOn") == "render";
needAsyncContext = completeOnRender || allBindings['has'](ko.bindingEvent.descendantsComplete);
@ -24,11 +30,13 @@ function makeIfBinding(bindingKey, isNot) {
}
if (shouldDisplay) {
if (renderOnEveryChange) {
if (!isWith || renderOnEveryChange) {
contextOptions['dataDependency'] = ko.computedContext.computed();
}
if (ko.computedContext.getDependenciesCount()) {
if (isWith) {
childContext = bindingContext['createChildContext'](typeof value == "function" ? value : valueAccessor, contextOptions);
} else if (ko.computedContext.getDependenciesCount()) {
childContext = bindingContext['extend'](null, contextOptions);
} else {
childContext = bindingContext;
@ -66,7 +74,8 @@ function makeIfBinding(bindingKey, isNot) {
}
// Construct the actual binding handlers
makeIfBinding('if');
makeIfBinding('ifnot', true);
makeWithIfBinding('if');
makeWithIfBinding('ifnot', false /* isWith */, true /* isNot */);
makeWithIfBinding('with', true /* isWith */);
})();