-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure all descendant bindings of
if
, with
, etc. have a depend…
…ency on the parent condition so that updates happen in the right order when using deferred updates.
- Loading branch information
Showing
5 changed files
with
87 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
// Makes a binding like with or if | ||
function makeWithIfBinding(bindingKey, isWith, isNot, makeContextCallback) { | ||
function makeWithIfBinding(bindingKey, isWith, isNot) { | ||
ko.bindingHandlers[bindingKey] = { | ||
'init': function(element, valueAccessor, allBindings, viewModel, bindingContext) { | ||
var didDisplayOnLastUpdate, | ||
savedNodes; | ||
var savedNodes; | ||
var ifCondition = !isWith && ko.computed(function() { | ||
return !isNot !== !ko.utils.unwrapObservable(valueAccessor()); | ||
}, null, { disposeWhenNodeIsRemoved: element }); | ||
|
||
ko.computed(function() { | ||
var rawValue = valueAccessor(), | ||
dataValue = ko.utils.unwrapObservable(rawValue), | ||
shouldDisplay = !isNot !== !dataValue, // equivalent to isNot ? !dataValue : !!dataValue | ||
isFirstRender = !savedNodes, | ||
needsRefresh = isFirstRender || isWith || (shouldDisplay !== didDisplayOnLastUpdate); | ||
var shouldDisplay = isWith ? !!ko.utils.unwrapObservable(valueAccessor()) : ifCondition(), | ||
isFirstRender = !savedNodes; | ||
|
||
if (needsRefresh) { | ||
// Save a copy of the inner nodes on the initial update, but only if we have dependencies. | ||
if (isFirstRender && ko.computedContext.getDependenciesCount()) { | ||
savedNodes = ko.utils.cloneNodes(ko.virtualElements.childNodes(element), true /* shouldCleanNodes */); | ||
} | ||
// Save a copy of the inner nodes on the initial update, but only if we have dependencies. | ||
if (isFirstRender && ko.computedContext.getDependenciesCount()) { | ||
savedNodes = ko.utils.cloneNodes(ko.virtualElements.childNodes(element), true /* shouldCleanNodes */); | ||
} | ||
|
||
if (shouldDisplay) { | ||
if (!isFirstRender) { | ||
ko.virtualElements.setDomNodeChildren(element, ko.utils.cloneNodes(savedNodes)); | ||
} | ||
ko.applyBindingsToDescendants(makeContextCallback ? makeContextCallback(bindingContext, rawValue) : bindingContext, element); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mblarsen
Contributor
|
||
} else { | ||
ko.virtualElements.emptyNode(element); | ||
if (shouldDisplay) { | ||
if (!isFirstRender) { | ||
ko.virtualElements.setDomNodeChildren(element, ko.utils.cloneNodes(savedNodes)); | ||
} | ||
|
||
didDisplayOnLastUpdate = shouldDisplay; | ||
ko.applyBindingsToDescendants( | ||
isWith ? | ||
bindingContext['createChildContext'](valueAccessor) : | ||
ifCondition.isActive() ? | ||
new ko.bindingContext(ko.bindingContext.inheritParentVm, bindingContext, null, function() { ifCondition(); }) : | ||
bindingContext, | ||
element); | ||
} else { | ||
ko.virtualElements.emptyNode(element); | ||
} | ||
}, null, { disposeWhenNodeIsRemoved: element }); | ||
|
||
return { 'controlsDescendantBindings': true }; | ||
} | ||
}; | ||
|
@@ -39,8 +42,4 @@ function makeWithIfBinding(bindingKey, isWith, isNot, makeContextCallback) { | |
// Construct the actual binding handlers | ||
makeWithIfBinding('if'); | ||
makeWithIfBinding('ifnot', false /* isWith */, true /* isNot */); | ||
makeWithIfBinding('with', true /* isWith */, false /* isNot */, | ||
function(bindingContext, dataValue) { | ||
return bindingContext.createStaticChildContext(dataValue); | ||
} | ||
); | ||
makeWithIfBinding('with', true /* isWith */); |
I'm rebasing this PR b7e391e#diff-1d8c93790f19c00e4fd280d3a5d13241L24 to this code and trying to get back into it.
Anyway, the PR adds an optional
as:
binding towith:
. What would be the way to add such binding now thatmakeContextCallback
is gone?