Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-conditional-binding-async-completion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tko/binding.if": patch
---

Fix async completion in ConditionalBindingHandler: call completeBinding() for non-rendering branches (if: false, ifnot: true, with: null) and correctly await async descendants via completionPromise instead of awaiting the BindingResult directly.
10 changes: 10 additions & 0 deletions packages/bind/spec/bindingCompletionPromiseBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,14 @@ describe('Binding Application Promise', function () {
'<div><span data-bind="template: {foreach: y}"><i data-bind="foreach: z"><i data-bind="text: $data">a</i><i data-bind="text: $data">b</i></i></span></div>'
)
})

it('completes with if binding containing async descendants', async function () {
const div = document.createElement('div')
div.innerHTML = '<span data-bind="if: true"><i data-bind="async: x"></i></span>'
const x = observable(false)
const abr = applyBindings({ x }, div)
expect(x()).to.equal(false)
await abr
expect(x()).to.equal(true)
})
})
6 changes: 6 additions & 0 deletions packages/binding.if/spec/elseBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,10 @@ describe('Else binding', function () {
y(false)
expect(testNode.innerText).to.equal('x')
})

it('Should resolve applyBindings promise when if is false with an else sibling chain', async function () {
testNode.innerHTML = "<i data-bind='if: x'>a</i>" + "<b data-bind='else'>b</b>"
await applyBindings({ x: false }, testNode)
expect(testNode.innerText).to.equal('b')
})
})
6 changes: 6 additions & 0 deletions packages/binding.if/spec/ifBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,10 @@ describe('Binding: If', function () {
expect(callbacks).to.equal(2)
expectContainText(testNode.childNodes[0], 'hello')
})

it('Should resolve applyBindings promise when condition is false with no else branch', async function () {
testNode.innerHTML = "<div data-bind='if: false'><span data-bind='text: $data'></span></div>"
await applyBindings({}, testNode)
expect(testNode.childNodes[0].childNodes.length).to.equal(0)
})
})
6 changes: 6 additions & 0 deletions packages/binding.if/spec/ifnotBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ describe('Binding: Ifnot', function () {
expect(callbacks).to.equal(2)
expectContainText(testNode.childNodes[0], 'hello')
})

it('Should resolve applyBindings promise when condition is truthy with no else branch', async function () {
testNode.innerHTML = "<div data-bind='ifnot: true'><span data-bind='text: $data'></span></div>"
await applyBindings({}, testNode)
expect(testNode.childNodes[0].childNodes.length).to.equal(0)
})
})
6 changes: 6 additions & 0 deletions packages/binding.if/spec/withBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,10 @@ describe('Binding: With', function () {
expect(callbacks).to.equal(2)
expectContainText(testNode.childNodes[0], 'new child')
})

it('Should resolve applyBindings promise when value is null with no else branch', async function () {
testNode.innerHTML = "<div data-bind='with: item'><span data-bind='text: $data'></span></div>"
await applyBindings({ item: null }, testNode)
expect(testNode.childNodes[0].childNodes.length).to.equal(0)
})
})
8 changes: 7 additions & 1 deletion packages/binding.if/src/ConditionalBindingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ export default class ConditionalBindingHandler extends AsyncBindingHandler {
this.renderAndApplyBindings(this.ifElseNodes.elseNodes)
} else {
virtualElements.emptyNode(this.$element)
this.completeBinding()
}
}

async renderAndApplyBindings(nodes: ArrayLike<Node>, useOriginalNodes?: boolean) {
if (!useOriginalNodes) {
virtualElements.setDomNodeChildren(this.$element, cloneNodes(nodes))
}
const bound = await applyBindingsToDescendants(this.bindingContext, this.$element)
const bound = applyBindingsToDescendants(this.bindingContext, this.$element)
if (bound.isSync) {
this.bindingCompletion = bound
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used for anything? Seems new, but unused.

Would this be sufficient?

if (!bound.isSync) {
  await bound.completionPromise
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L.93 must be called with bound

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
await bound.completionPromise
}
this.completeBinding(bound)
}

Expand Down
Loading