Skip to content

Commit

Permalink
afterRender binding works with any binding that calls ko.applyBinding…
Browse files Browse the repository at this point in the history
…sToDescendants or with just normal binding processing (more tests to come).
  • Loading branch information
mbest committed Oct 12, 2017
1 parent d7fc725 commit 4740bb3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
17 changes: 16 additions & 1 deletion src/binding/bindingAttributeSyntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@
throw new Error("The binding '" + bindingName + "' cannot be used with virtual elements")
}

var afterRenderCallbackDomDataKey = ko.utils.domData.nextKey();

function applyBindingsToDescendantsInternal(bindingContext, elementOrVirtualElement) {
var nextInQueue = ko.virtualElements.firstChild(elementOrVirtualElement);

Expand All @@ -230,6 +232,16 @@
nextInQueue = ko.virtualElements.nextSibling(currentChild);
applyBindingsToNodeAndDescendantsInternal(bindingContext, currentChild);
}

var afterRender = ko.utils.domData.get(elementOrVirtualElement, afterRenderCallbackDomDataKey);
if (afterRender) {
var nodes = ko.virtualElements.childNodes(elementOrVirtualElement);
if (nodes.length) {
ko.dependencyDetection.ignore(function () {
evaluateValueAccessor(afterRender)(nodes, ko.dataFor(nodes[0]));
});
}
}
}
}

Expand All @@ -254,7 +266,6 @@

var boundElementDomDataKey = ko.utils.domData.nextKey();


function topologicalSortBindings(bindings) {
// Depth-first sort
var result = [], // The list of key/handler pairs that we will return
Expand Down Expand Up @@ -352,6 +363,10 @@
return key in bindings;
};

if ("afterRender" in bindings) {
ko.utils.domData.set(node, afterRenderCallbackDomDataKey, getValueAccessor("afterRender"));
}

// First put the bindings into the right order
var orderedBindings = topologicalSortBindings(bindings);

Expand Down
17 changes: 7 additions & 10 deletions src/binding/defaultBindings/ifIfnotWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@ function makeWithIfBinding(bindingKey, isWith, isNot) {
ko.computed(function() {
var rawWithValue = isWith && ko.utils.unwrapObservable(valueAccessor()),
shouldDisplay = isWith ? !!rawWithValue : ifCondition(),
isFirstRender = !savedNodes,
renderNodes;
isFirstRender = !savedNodes;

// 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(renderNodes = ko.virtualElements.childNodes(element), true /* shouldCleanNodes */);
savedNodes = ko.utils.cloneNodes(ko.virtualElements.childNodes(element), true /* shouldCleanNodes */);
}

if (shouldDisplay) {
if (!isFirstRender) {
ko.virtualElements.setDomNodeChildren(element, renderNodes = ko.utils.cloneNodes(savedNodes));
ko.virtualElements.setDomNodeChildren(element, ko.utils.cloneNodes(savedNodes));
}
var newContext = isWith ?
ko.applyBindingsToDescendants(
isWith ?
bindingContext['createChildContext'](typeof rawWithValue == "function" ? rawWithValue : valueAccessor) :
ifCondition.isActive() ?
bindingContext['extend'](function() { ifCondition(); return null; }) :
bindingContext;
ko.applyBindingsToDescendants(newContext, element);
if (allBindings.has('afterRender')) {
ko.dependencyDetection.ignore(allBindings.get('afterRender'), null, [renderNodes || ko.virtualElements.childNodes(element), newContext['$data']]);
}
bindingContext,
element);
} else {
ko.virtualElements.emptyNode(element);
}
Expand Down

0 comments on commit 4740bb3

Please sign in to comment.