From 11182a3b5ac2dabbd69fe285ea8d7b04923bad79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20B=C3=B8cker-Larsen?= Date: Fri, 10 Jun 2016 22:08:15 +0800 Subject: [PATCH] `as:` binding can be used with `with:` --- spec/defaultBindings/withBehaviors.js | 36 +++++++++++++++++++++- src/binding/defaultBindings/ifIfnotWith.js | 4 +-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/spec/defaultBindings/withBehaviors.js b/spec/defaultBindings/withBehaviors.js index a9df2293b..aa6b8907a 100644 --- a/spec/defaultBindings/withBehaviors.js +++ b/spec/defaultBindings/withBehaviors.js @@ -134,6 +134,40 @@ describe('Binding: With', function() { expect(ko.contextFor(firstSpan).$parents[1].name).toEqual("top"); }); + it('Should be able to access all parent binding when using `as:`', function() { + testNode.innerHTML = "
" + + "
" + + "
" + + "" + + "" + + "" + + "" + + "" + + "" + + "
" + + "
" + + "
"; + ko.applyBindings({ + name: 'outer', + topItem: { + name: 'top', + middleItem: { + name: 'middle', + bottomItem: { + name: "bottom" + } + } + } + }, testNode); + var finalContainer = testNode.childNodes[0].childNodes[0].childNodes[0]; + expect(finalContainer.childNodes[0]).toContainText("bottom"); + expect(finalContainer.childNodes[1]).toContainText("middle"); + expect(finalContainer.childNodes[2]).toContainText("middle"); + expect(finalContainer.childNodes[3]).toContainText("top"); + expect(finalContainer.childNodes[4]).toContainText("outer"); + expect(finalContainer.childNodes[5]).toContainText("outer"); + }); + it('Should be able to define an \"with\" region using a containerless template', function() { var someitem = ko.observable(undefined); testNode.innerHTML = "hello goodbye"; @@ -324,4 +358,4 @@ describe('Binding: With', function() { ko.bindingProvider.instance = originalBindingProvider; }); -}); \ No newline at end of file +}); diff --git a/src/binding/defaultBindings/ifIfnotWith.js b/src/binding/defaultBindings/ifIfnotWith.js index 3d1a39aff..2ff6bc154 100755 --- a/src/binding/defaultBindings/ifIfnotWith.js +++ b/src/binding/defaultBindings/ifIfnotWith.js @@ -23,7 +23,7 @@ function makeWithIfBinding(bindingKey, isWith, isNot) { ko.virtualElements.setDomNodeChildren(element, renderNodes = ko.utils.cloneNodes(savedNodes)); } var newContext = isWith ? - bindingContext['createChildContext'](typeof rawWithValue == "function" ? rawWithValue : valueAccessor) : + bindingContext['createChildContext'](typeof rawWithValue == "function" ? rawWithValue : valueAccessor, allBindings.get('as')) : ifCondition.isActive() ? bindingContext['extend'](function() { ifCondition(); return null; }) : bindingContext; @@ -46,4 +46,4 @@ function makeWithIfBinding(bindingKey, isWith, isNot) { // Construct the actual binding handlers makeWithIfBinding('if'); makeWithIfBinding('ifnot', false /* isWith */, true /* isNot */); -makeWithIfBinding('with', true /* isWith */); +makeWithIfBinding('with', true /* isWith */, false /* isNot */);