Skip to content

Commit

Permalink
Minor stylistic tweaks to new specs
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Feb 2, 2012
1 parent ab8087e commit 4c96776
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions spec/bindingAttributeBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,93 +236,99 @@ describe('Binding attribute syntax', {
},

'Should be able to set a custom binding to use containerless binding': function() {
var initCalls = 0;
ko.bindingHandlers.test = { init: function () { initCalls++ } };
ko.virtualElements.allowedBindings['test'] = true;

testNode.innerHTML = "Hello <!-- ko test: false -->Some text<!-- /ko --> Goodbye"
var didThrow = false, initCalls = 0;
ko.bindingHandlers.test = { init: function (element, valueAccessor) { initCalls++; } };
try {
ko.applyBindings(null, testNode);
} catch(ex) {
didThrow = true;
}
ko.applyBindings(null, testNode);

value_of(initCalls).should_be(1);
value_of(didThrow).should_be(false);
value_of(testNode).should_contain_text("Hello Some text Goodbye");

delete ko.virtualElements.allowedBindings['test'];
},

'Should be able to access virtual children in custom containerless binding': function() {
var countNodes = 0;
ko.bindingHandlers.test = {
init: function (element, valueAccessor) {
// Counts the number of virtual children, and overwrites the text contents of any text nodes
for (var node = ko.virtualElements.firstChild(element); node; node = ko.virtualElements.nextSibling(node)) {
countNodes++;
if (node.nodeType === 3)
node.data = 'new text';
}
}
};
ko.virtualElements.allowedBindings['test'] = true;

testNode.innerHTML = "Hello <!-- ko test: false -->Some text<!-- /ko --> Goodbye"
var countNodes = 0;
ko.bindingHandlers.test = { init: function (element, valueAccessor) {
for (var node = ko.virtualElements.firstChild(element); node; node = ko.virtualElements.nextSibling(node)) {
countNodes++;
if (node.nodeType === 3)
node.data = 'new text';
}
} };
ko.applyBindings(null, testNode);

value_of(countNodes).should_be(1);
value_of(testNode).should_contain_text("Hello new text Goodbye");

delete ko.virtualElements.allowedBindings['test'];
},

'Should only bind containerless binding once inside template': function() {
var initCalls = 0;
ko.bindingHandlers.test = { init: function () { initCalls++ } };
ko.virtualElements.allowedBindings['test'] = true;

testNode.innerHTML = "Hello <!-- if: true --><!-- ko test: false -->Some text<!-- /ko --><!-- /ko --> Goodbye"
var initCalls = 0;
ko.bindingHandlers.test = { init: function (element, valueAccessor) { initCalls++; } };
ko.applyBindings(null, testNode);

value_of(initCalls).should_be(1);
value_of(testNode).should_contain_text("Hello Some text Goodbye");

delete ko.virtualElements.allowedBindings['test'];
},

'Should be able to set and access correct context in custom containerless binding': function() {
ko.virtualElements.allowedBindings['test'] = true;

testNode.innerHTML = "Hello <!-- ko test: false --><div>Some text</div><!-- /ko --> Goodbye"
var innerContext = new ko.bindingContext();
ko.bindingHandlers.test = { init: function (element, valueAccessor) {
ko.applyBindingsToDescendants(innerContext, element, true);
return { 'controlsDescendantBindings': true };
} };
ko.bindingHandlers.bindChildrenWithCustomContext = {
init: function (element) {
ko.applyBindingsToDescendants(innerContext, element, true);
return { 'controlsDescendantBindings': true };
}
};
ko.virtualElements.allowedBindings['bindChildrenWithCustomContext'] = true;

testNode.innerHTML = "Hello <!-- ko bindChildrenWithCustomContext: true --><div>Some text</div><!-- /ko --> Goodbye"
ko.applyBindings(null, testNode);
value_of(ko.contextFor(testNode.childNodes[2])).should_be(innerContext);

delete ko.virtualElements.allowedBindings['test'];
value_of(ko.contextFor(testNode.childNodes[2])).should_be(innerContext);
},

'Should be able to set and access correct context in nested containerless binding': function() {
testNode.innerHTML = "Hello <div data-bind='test: false'><!-- ko dummy: false --><div>Some text</div><!-- /ko --></div> Goodbye"
var innerContext = new ko.bindingContext();
ko.bindingHandlers.test = { init: function (element, valueAccessor) {
ko.applyBindingsToDescendants(innerContext, element, true);
return { 'controlsDescendantBindings': true };
} };
delete ko.bindingHandlers.nonexistentHandler;
ko.bindingHandlers.bindChildrenWithCustomContext = {
init: function (element) {
ko.applyBindingsToDescendants(innerContext, element, true);
return { 'controlsDescendantBindings': true };
}
};

testNode.innerHTML = "Hello <div data-bind='bindChildrenWithCustomContext: true'><!-- ko nonexistentHandler: 123 --><div>Some text</div><!-- /ko --></div> Goodbye"
ko.applyBindings(null, testNode);

value_of(ko.contextFor(testNode.childNodes[1].childNodes[0])).should_be(innerContext);
value_of(ko.contextFor(testNode.childNodes[1].childNodes[1])).should_be(innerContext);
},

'Should be able to access custom context variables in child context': function() {
testNode.innerHTML = "Hello <div data-bind='test: false'><!-- ko with: data --><div>Some text</div><!-- /ko --></div> Goodbye"
var innerContext = ko.utils.extend(new ko.bindingContext({data: {}}), {custom: true});
ko.bindingHandlers.test = { init: function (element, valueAccessor) {
ko.applyBindingsToDescendants(innerContext, element, true);
return { 'controlsDescendantBindings': true };
} };
var innerContext = new ko.bindingContext({ myModelValue: {} });
innerContext.customValue = 123;
ko.bindingHandlers.bindChildrenWithCustomContext = {
init: function (element) {
ko.applyBindingsToDescendants(innerContext, element, true);
return { 'controlsDescendantBindings': true };
}
};

testNode.innerHTML = "Hello <div data-bind='bindChildrenWithCustomContext: true'><!-- ko with: myModelValue --><div>Some text</div><!-- /ko --></div> Goodbye"
ko.applyBindings(null, testNode);

value_of(ko.contextFor(testNode.childNodes[1].childNodes[0])).should_be(innerContext);
value_of(ko.contextFor(testNode.childNodes[1].childNodes[1]).$parent).should_be(innerContext.$data);
value_of(ko.contextFor(testNode.childNodes[1].childNodes[1]).$parentContext.custom).should_be(true);
value_of(ko.contextFor(testNode.childNodes[1].childNodes[1]).$parentContext.customValue).should_be(123);
},

'Should not reinvoke init for notifications triggered during first evaluation': function () {
Expand Down

0 comments on commit 4c96776

Please sign in to comment.