Skip to content

Commit

Permalink
clean up ifIfnotWith
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Oct 10, 2018
1 parent 5a8dc4d commit c4589c4
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/binding/defaultBindings/ifIfnotWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@
function makeWithIfBinding(bindingKey, isWith, isNot) {
ko.bindingHandlers[bindingKey] = {
'init': function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var savedNodes, contextOptions = {}, ifCondition, completeOnRender, needAsyncContext, renderOnEveryChange;
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 };
}

if (!renderOnEveryChange) {
ifCondition = ko.computed(function() {
return !isNot !== !ko.utils.unwrapObservable(valueAccessor());
}, null, { disposeWhenNodeIsRemoved: element });
}

completeOnRender = allBindings.get("completeOn") == "render";
needAsyncContext = completeOnRender || allBindings['has'](ko.bindingEvent.descendantsComplete);

ko.computed(function() {
var value = ifCondition ? ifCondition() : ko.utils.unwrapObservable(valueAccessor()),
shouldDisplay = !!value,
isFirstRender = !savedNodes,
var value = ko.utils.unwrapObservable(valueAccessor()),
shouldDisplay = !isNot !== !value, // equivalent to isNot ? !value : !!value,
isInitial = !savedNodes,
childContext;

if (!renderOnEveryChange && shouldDisplay === didDisplayOnLastUpdate) {
return;
}

if (needAsyncContext) {
bindingContext = ko.bindingEvent.startPossiblyAsyncContentBinding(element, bindingContext);
}
Expand All @@ -38,20 +36,20 @@ function makeWithIfBinding(bindingKey, isWith, isNot) {

if (isWith) {
childContext = bindingContext['createChildContext'](typeof value == "function" ? value : valueAccessor, contextOptions);
} else if (ifCondition.isActive()) {
} else if (ko.computedContext.getDependenciesCount()) {
childContext = bindingContext['extend'](null, contextOptions);
} else {
childContext = bindingContext;
}
}

// Save a copy of the inner nodes on the initial update, but only if we have dependencies.
if (isFirstRender && ko.computedContext.getDependenciesCount()) {
if (isInitial && ko.computedContext.getDependenciesCount()) {
savedNodes = ko.utils.cloneNodes(ko.virtualElements.childNodes(element), true /* shouldCleanNodes */);
}

if (shouldDisplay) {
if (!isFirstRender) {
if (!isInitial) {
ko.virtualElements.setDomNodeChildren(element, ko.utils.cloneNodes(savedNodes));
}

Expand All @@ -63,6 +61,9 @@ function makeWithIfBinding(bindingKey, isWith, isNot) {
ko.bindingEvent.notify(element, ko.bindingEvent.childrenComplete);
}
}

didDisplayOnLastUpdate = shouldDisplay;

}, null, { disposeWhenNodeIsRemoved: element });

return { 'controlsDescendantBindings': true };
Expand Down

0 comments on commit c4589c4

Please sign in to comment.