From 6267ba98e2dac0e73d3c61555a1a69091beebd83 Mon Sep 17 00:00:00 2001 From: Aaron Stacy Date: Thu, 17 Oct 2013 09:42:32 -0500 Subject: [PATCH] preserve the oa instance when it appears in data this may be easiest to illustrate with code: ```js var o = ko.observable() var oa = ko.observableArray() var model = {}; ko.mapping.fromJS({o: o, oa: oa}, model); equal(model.o, o); // this is currently true equal(model.oa, oa) // this isn't true, but is fixed with this commit ``` the problem is that using ko.mapping, if you've got an existing OA, there's no way to update a viewmodel with the existing OA, a new one is always created. this pull request provides a way to update viewmodels to reference pre-existing observable arrays. --- knockout.mapping.js | 2 +- spec/issues.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/knockout.mapping.js b/knockout.mapping.js index 2ac4a3d..719a04c 100644 --- a/knockout.mapping.js +++ b/knockout.mapping.js @@ -503,7 +503,7 @@ if (!ko.isObservable(mappedRootObject)) { // When creating the new observable array, also add a bunch of utility functions that take the 'key' of the array items into account. - mappedRootObject = ko.observableArray([]); + mappedRootObject = exports.getType(rootObject) === "function" ? rootObject : ko.observableArray([]); mappedRootObject.mappedRemove = function (valueOrPredicate) { var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { diff --git a/spec/issues.js b/spec/issues.js index 3ac3ea8..bad8575 100644 --- a/spec/issues.js +++ b/spec/issues.js @@ -314,3 +314,14 @@ test('Issue #107', function () { equal(model.foo(), "baz"); }); + +//https://github.com/SteveSanderson/knockout.mapping/issues/177 +test('Issue #177', function () { + var model = ko.mapping.fromJS({}); + var o = ko.observable(); + var oa = ko.observableArray(); + + ko.mapping.fromJS({o: o, oa: oa}, model); + equal(model.o, o); + equal(model.oa, oa); +});