Complex Object passed into ko.mapping.fromJS
javascript, jquery, knockout-2.0, knockout-mapping-plugin, knockout.js
Solution
You need to use `copy` instead of `ignore` because you want to have properties there just not be observable.
And because you are mapping directly an array the mapping configuration becomes a little bit complicated.
You cannot define `copy` on the "root" level because you have the array at the root. So you have to supply a `create` function for the items and in the create function you can now specify the `copy` options for the properties of the item:
ko.mapping.fromJS(
[
{ 'qty': 1, 'inner': { 'name': 'thing'} },
{ 'qty': 2, 'inner': { 'name': 'stuff'} }
],
{
create: function (options) {
return ko.mapping.fromJS(options.data, {
copy: ['inner.name']
})
}
},
self.slots);
Demo JSFiddle.
Problem
I have a complex object I want to pass into `ko.mapping.fromJS` and my problem is that I only want one field to be observable, but the other properties come across as either null or non-existent based on the methods I have tried. I have created a jsFiddle here to illustrate my problem. I wish for the inner object to simply be copied as I have no need for it to be observable; I don't want the extra overhead considering the number of these I will have. The goal of this would be to make the `qty` editable, but the `inner.name` remain the same in the text box. This would mean that one is an observable while the other is not. If any one has another way of doing it that does not involve the mapping I would love to hear it. My view model has quite a few functions and such, and the data is coming in from an AJAX call. ``` function viewModel() { var self = this; self.slots = ko.observableArray([]); self.load = function() { ko.mapping.fromJS( [ { 'qty': 1, 'inner': { 'name': 'thing'} }, { 'qty': 2, 'inner': { 'name': 'stuff'} } ], { 'include': ['qty'], 'ignore': ['inner.name'] }, self.slots); } }; ko.applyBindings(new viewModel()); <button data-bind="click: load">Go</button> <ul data-bind="foreach: slots"> <li> <span data-bind="text: qty"></span> <span data-bind="text: inner.name"></span><input data-bind="value: qty" /><input data-bind="value: inner.name" /> </li> </ul> ```