Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support writing to non-observable items in an array; add spec for wri…

…ting
  • Loading branch information...
commit 2bb3750d4856e757ee8dea488d8fbdd3686a9388 1 parent 2c03f9d
@mbest authored
Showing with 24 additions and 3 deletions.
  1. +5 −3 knockout-repeat.js
  2. +19 −0 spec/repeatBinding.js
View
8 knockout-repeat.js
@@ -1,7 +1,7 @@
// REPEAT binding for Knockout http://knockoutjs.com/
// (c) Michael Best
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
-// Version 1.4.1
+// Version 1.4.2
(function(factory) {
if (typeof define === 'function' && define['amd']) {
@@ -78,13 +78,15 @@ ko.bindingHandlers['repeat'] = {
var subscribable = ko.dependentObservable(function() {
function makeArrayItemAccessor(index) {
- var f = function() {
+ var f = function(newValue) {
var item = repeatArray[index];
if (!arguments.length) {
notificationObservable(); // for dependency tracking
return ko.utils.unwrapObservable(item);
} else if (ko.isObservable(item)) {
- return item(arguments[0]);
+ return item(newValue);
+ } else {
+ repeatArray[index] = newValue;
}
};
// Pretend that our accessor function is an observable
View
19 spec/repeatBinding.js
@@ -185,6 +185,25 @@ describe('Binding: Repeat', {
value_of(testNode).should_contain_text('A2B');
},
+ 'Should be able to update observable and non-observable items using a two-way binding': function() {
+ testNode.innerHTML = "<div data-bind='repeat: someitems'><input data-bind='value: $item' /></div>";
+ var someitems = [ ko.observable('A'), 'B' ];
+ ko.applyBindings({ someitems: someitems }, testNode);
+
+ value_of(testNode.childNodes[0].childNodes[0].value).should_be('A');
+ value_of(testNode.childNodes[1].childNodes[0].value).should_be('B');
+
+ // Now update the value of the observable item through the input
+ testNode.childNodes[0].childNodes[0].value = 'X';
+ ko.utils.triggerEvent(testNode.childNodes[0].childNodes[0], "change");
+ value_of(someitems[0]()).should_be('X');
+
+ // and now the non-observable item
+ testNode.childNodes[1].childNodes[0].value = 'Y';
+ ko.utils.triggerEvent(testNode.childNodes[1].childNodes[0], "change");
+ value_of(someitems[1]).should_be('Y');
+ },
+
'Should be able to nest \'repeat\' and access binding contexts both during and after binding': function() {
testNode.innerHTML = "<div data-bind='repeat: items'>"
+ "<div data-bind='repeat: {foreach: $item().children, item: \"$child\"}'>"
Please sign in to comment.
Something went wrong with that request. Please try again.