Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Explain in mapping's documentation how to fill an array after it is initialized #297

Closed
kappa7194 opened this Issue · 3 comments

4 participants

@kappa7194

This issue is based on a Stackoverflow question I asked today.

I think it would be useful to expand the mapping's documentation to explain how can you fill an array property after it is initialized.

Note how self.items must be initialized with ko.mapping.fromJS([]); and not with ko.observableArray(); as using the latter won't work, I haven't been able to find this in the documentation (feel free to point me to it if I missed it).

@jamesfoster

You can use ko.observableArray() you just need to specify an empty options object like ko.mapping.fromJS(data, {}, self.items) (as shown in the second answer on SO)

The only way to use the 2 argument form ko.mapping.fromJS(data, self.items) is if self.items was previously mapped using a call to fromJS or fromJSON

I agree, this could possibly be made clearer in the documentation. Perhaps the examples should all use the 3 argument to avoid any confusion?

@JakeWharton

Upon first adoption of knockout a few months ago I was too tripped up by this initially. I managed to stumble on the solution from my knowledge of some of jQuery's similar commands. Having clearer documentation would probably help newbs.

@SteveSanderson

Moved to SteveSanderson/knockout.mapping#49. Thanks for suggesting this!

(Marking as not-a-bug only because it's not a KO core bug - this is just for KO core tracking purposes and does not invalidate your suggestion!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.