简体   繁体   English

淘汰JS:mapping.fromJS添加时会去除对象的可观察属性

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

I have the following ko.mapping and an object (coffeescript): 我有以下ko.mapping和一个对象(咖啡脚本):

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. 我已经验证了敲除的可观察函数可以按预期填充obj If I call obj.name() I get the expected value back, for example. 例如,如果我调用obj.name()得到期望值。

However, when I do form_fields.push obj OR ko.mapping.fromJS [obj], form_fields , form_fields is populated with an empty object. 但是,当我执行form_fields.push objko.mapping.fromJS [obj], form_fieldsform_fields填充了一个空对象。 The object exists within the array, but it has no attributes at all. 该对象存在于数组中,但根本没有属性。

Here's the resulting array in the chrome dev tools: 这是chrome开发工具中的结果数组:

开发工具对象

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. 中间的空对象是映射规则 ,即指定映射目标时需要的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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