Skip to content

Commit

Permalink
Fix for knockout#63 (improve observableArray constructor behavior)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Mar 26, 2011
1 parent 9fcd3de commit 11749c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
6 changes: 6 additions & 0 deletions build/output/knockout-latest.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,12 @@ ko.exportSymbol('ko.isObservable', ko.isObservable);
ko.exportSymbol('ko.isWriteableObservable', ko.isWriteableObservable);

ko.observableArray = function (initialValues) {
if (arguments.length == 0) {
// Zero-parameter constructor initializes to empty array
initialValues = [];
}
if ((initialValues !== null) && (initialValues !== undefined) && !('length' in initialValues))
throw new "The argument passed when initializing an observable array must be an array, or null, or undefined.";
var result = new ko.observable(initialValues);

ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
Expand Down
6 changes: 3 additions & 3 deletions build/output/knockout-latest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions spec/observableArrayBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ describe('Observable Array', {
'Should be observable': function () {
value_of(ko.isObservable(testObservableArray)).should_be(true);
},

'Should initialize to empty array if you pass no args to constructor' : function() {
var instance = new ko.observableArray();
value_of(instance().length).should_be(0);
},

'Should require constructor arg, if given, to be array-like or null or undefined' : function() {
// Try non-array-like args
var threw;
try { threw = false; new ko.observableArray(1); } catch(ex) { threw = true }
value_of(threw).should_be(true);
try { threw = false; new ko.observableArray({}); } catch(ex) { threw = true }
value_of(threw).should_be(true);

// Try allowed args
value_of((new ko.observableArray([1,2,3]))().length).should_be(3);
value_of((new ko.observableArray(null))()).should_be(null);
value_of((new ko.observableArray(undefined))()).should_be(undefined);
},

'Should be able to write values to it': function () {
testObservableArray(['X', 'Y']);
Expand Down
6 changes: 6 additions & 0 deletions src/subscribables/observableArray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

ko.observableArray = function (initialValues) {
if (arguments.length == 0) {
// Zero-parameter constructor initializes to empty array
initialValues = [];
}
if ((initialValues !== null) && (initialValues !== undefined) && !('length' in initialValues))
throw new "The argument passed when initializing an observable array must be an array, or null, or undefined.";
var result = new ko.observable(initialValues);

ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
Expand Down

0 comments on commit 11749c3

Please sign in to comment.