From e3fce055af2ee01f9f6691a76a8bc2d93109422d Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Tue, 5 Feb 2013 21:38:51 +0100 Subject: [PATCH 1/2] fixes #733 --- src/subscribables/observableArray.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/subscribables/observableArray.js b/src/subscribables/observableArray.js index fd972e17d..1078848c6 100644 --- a/src/subscribables/observableArray.js +++ b/src/subscribables/observableArray.js @@ -1,9 +1,9 @@ ko.observableArray = function (initialValues) { - if (arguments.length == 0) { + if ((initialValues === undefined) || (initialValues === null)) { // Zero-parameter constructor initializes to empty array initialValues = []; } - if ((initialValues !== null) && (initialValues !== undefined) && !('length' in initialValues)) + if (!('length' in initialValues)) throw new Error("The argument passed when initializing an observable array must be an array, or null, or undefined."); var result = ko.observable(initialValues); From 43ab750d0001d28cd61d913566f1aecf14631865 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Tue, 26 Feb 2013 20:36:09 +0100 Subject: [PATCH 2/2] better handling of some cases --- src/subscribables/observableArray.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/subscribables/observableArray.js b/src/subscribables/observableArray.js index 1078848c6..2d6c63204 100644 --- a/src/subscribables/observableArray.js +++ b/src/subscribables/observableArray.js @@ -1,15 +1,13 @@ ko.observableArray = function (initialValues) { - if ((initialValues === undefined) || (initialValues === null)) { - // Zero-parameter constructor initializes to empty array - initialValues = []; - } - if (!('length' in initialValues)) + initialValues = initialValues || []; + + if (typeof initialValues != 'object' || !('length' in initialValues)) throw new Error("The argument passed when initializing an observable array must be an array, or null, or undefined."); var result = ko.observable(initialValues); ko.utils.extend(result, ko.observableArray['fn']); return result; -} +}; ko.observableArray['fn'] = { 'remove': function (valueOrPredicate) { @@ -89,7 +87,7 @@ ko.observableArray['fn'] = { this.valueHasMutated(); } } -} +}; // Populate ko.observableArray.fn with read/write functions from native arrays // Important: Do not add any additional functions here that may reasonably be used to *read* data from the array