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

Conversation

aaronj1335
Copy link

this may be easiest to illustrate with code:

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.

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.
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant