简体   繁体   中英

Knockout JS: mapping.fromJS stripping out observable attributes of object when added

I have the following ko.mapping and an object (coffeescript):

form_fields = ko.mapping.fromJS []

obj =
  name: ko.observable 'some_name'
  type: ko.observable 'some_type'
  avail_values: ko.observableArray some_array
  vals: ko.observableArray some_other_array_of_observables

I've verified that obj is being populated as expected by knockout observable functions. If I call obj.name() I get the expected value back, for example.

However, when I do form_fields.push obj OR ko.mapping.fromJS [obj], form_fields , form_fields is populated with an empty object. The object exists within the array, but it has no attributes at all.

Here's the resulting array in the chrome dev tools:

开发工具对象

Do I need to do something else to accomplish this?

Use

ko.mapping.fromJS [obj], {}, form_fields

The empty object in the middle is the mapping rules , a parameter which is required when you specify a mapping target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM