[英]Knockout JS - Observable becomes undefined when added to an array
I have an object called purchase
that gets put into one of two observable arrays depending on its status. 我有一个名为
purchase
的对象,根据其状态将其放入两个可观察的数组之一。 The purchase
object has an observable called integrationStatus
that I default to a css class name of integration-status--complete
purchase
对象具有一个可观察到的称为integrationStatus
对象,我默认将其命名为integration-status--complete
的CSS类名
For some reason the purchases in the Scheduled array are coded green as expected but the purchase in the Unscheduled array throws an error as integrationStatus
is undefined 由于某种原因,Scheduled数组中的购买按预期的绿色编码,但是Unscheduled数组中的购买却引发错误,因为
integrationStatus
未定义
Thanks in advance 提前致谢
The issue is related to the mapping configuration within Schedule
. 该问题与
Schedule
的映射配置有关。 The mapping call ko.mapping.fromJS(data, { scheduleDays : scheduleDayMapping }, self)
instructs the utility to only customize the mapping of the scheduleDays
object from the data
variable. 映射调用
ko.mapping.fromJS(data, { scheduleDays : scheduleDayMapping }, self)
指示实用程序仅从data
变量自定义scheduleDays
对象的映射。
Since that configuration doesn't include an entry for unallocatedHolder
, the mapping utility uses the default observable creation behavior instead of the custom behavior. 由于该配置不包含
unallocatedHolder
的条目,因此映射实用程序使用默认的可观察创建行为而不是自定义行为。 The UnallocatedHolder
function is never called, so its purchases
array doesn't get the custom mapping. 永远不会调用
UnallocatedHolder
函数,因此它的purchases
数组不会获得自定义映射。 Without that mapping, integrationStatus
is not defined for those purchases. 没有该映射,就不会为这些购买定义
integrationStatus
。
I'd suggest defining a new mapping configuration for Schedule
like this: 我建议像这样为
Schedule
定义新的映射配置:
var scheduleMapping = {
"scheduleDays": scheduleDayMapping,
"unallocatedHolder": {
create: function(options) {
return new UnallocatedHolder(options.data);
}
}
};
Then, reference this new mapping configuration within Schedule
by updating the mapping line to 然后,通过将映射行更新为在
Schedule
引用此新映射配置
ko.mapping.fromJS(data, scheduleMapping, self);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.