Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve the oa instance when it appears in data #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion knockout.mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we know here that rootObject is an array (because isArray is true). if it's also a function, then we know it's an observableArray, so we want to preserve that reference, so if the viewmodel is updated, so is the original observable array.


mappedRootObject.mappedRemove = function (valueOrPredicate) {
var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) {
Expand Down
11 changes: 11 additions & 0 deletions spec/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});