简体   繁体   English

淘汰赛JS-当添加到数组时,Observable变得不确定

[英]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 提前致谢

JS Fiddle JS小提琴

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);

Revised Fiddle 修正的小提琴

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

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