Breeze导入,实体扩展属性为ko.observeArray()抛出错误

Breeze导入,实体扩展属性为ko.observeArray()抛出错误,breeze,Breeze,对于使用ko.observableArray()属性扩展的实体,与在构造函数中扩展为简单数组[]类型的实体,在导入过程中遇到错误 var customerCtor = function () { this.extendedProp = ko.observable(true); //this.extendedArray = ko.observableArray(); // causes error: Cannot write a value to a ko.computed unl

对于使用
ko.observableArray()
属性扩展的实体,与在构造函数中扩展为简单数组
[]
类型的实体,在导入过程中遇到错误

var customerCtor = function () {
    this.extendedProp = ko.observable(true);
    //this.extendedArray = ko.observableArray(); // causes error: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.
    this.extendedArray = []; // this works just fine
  };
我以Breeze v1.3.6 DocCode:exportImportTests.js“在本地存储整个缓存并还原”为起点创建了一个测试,下面是新的测试:

test("w/extended Customer, stash entire cache locally and restore", 3, function () {
  var em1 = newEm();

  var store = em1.metadataStore;
  // extend Customer with observables 
  var customerCtor = function () {
    this.extendedProp = ko.observable(true);
    this.extendedArray = ko.observableArray(); // causes error: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.
    //this.extendedArray = []; // but this will work just fine?
  };

  store.registerEntityTypeCtor("Customer", customerCtor);

  var expected = testData.primeTheCache(em1);
  // grab first Customer, push value onto extendedArray prop
  var custEntity = em1.getEntities(expected.customerType)[0];
  custEntity.extendedArray().push('some-value');  // even when defined as [], Breeze re-writes field as ko.observable

  var exportData = em1.exportEntities();

  var stashName = "stash_everything";
  window.localStorage.setItem(stashName, exportData);

  var importData = window.localStorage.getItem(stashName);
  var em2 = new EntityManager(); // virginal - so register ctor on this instance
  var store2 = em2.metadataStore;
  store2.registerEntityTypeCtor("Customer", customerCtor);
  em2.importEntities(importData);

  var entitiesInCache = em2.getEntities();
  var restoreCount = entitiesInCache.length;
  equal(restoreCount, expected.entityCount,
      "should have restored expected number of all entities");

  var restoredCustomer = em2.getEntities(expected.customerType)[0];
  ok(restoredCustomer.extendedProp(), 'extended property present');
  ok(restoredCustomer.extendedArray().length > 0, 'extended Array present and has data');
});
a
em2.重要性(importData)抛出错误:

Error: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.
at Error (<anonymous>)
at h [as extendedArray] (http://localhost:47595/Scripts/knockout-2.2.1.js:44:167)
at ctor.initializeEntityPrototype.proto.setProperty (http://localhost:47595/Scripts/breeze.debug.js:14634:31)
at updateTargetPropertyFromRaw (http://localhost:47595/Scripts/breeze.debug.js:13062:24)
at aspectName (http://localhost:47595/Scripts/breeze.debug.js:13025:13)
at Array.forEach (native)
at updateTargetFromRaw (http://localhost:47595/Scripts/breeze.debug.js:13023:19)
at em._inKeyFixup (http://localhost:47595/Scripts/breeze.debug.js:12601:17)
at Array.forEach (native)
at importEntityGroup (http://localhost:47595/Scripts/breeze.debug.js:12568:28)
错误:除非指定“写入”选项,否则无法将值写入ko.computed。如果要读取当前值,请不要传递任何参数。
错误()
在h处[作为扩展数组](http://localhost:47595/Scripts/knockout-2.2.1.js:44:167)
在ctor.initializeEntityPrototype.proto.setProperty(http://localhost:47595/Scripts/breeze.debug.js:14634:31)
在updateTargetPropertyFromRaw(http://localhost:47595/Scripts/breeze.debug.js:13062:24)
在aspectName(http://localhost:47595/Scripts/breeze.debug.js:13025:13)
at Array.forEach(本机)
在updateTargetFromRaw(http://localhost:47595/Scripts/breeze.debug.js:13023:19)
在em.\u inKeyFixup(http://localhost:47595/Scripts/breeze.debug.js:12601:17)
at Array.forEach(本机)
重要的群体(http://localhost:47595/Scripts/breeze.debug.js:12568:28)
由于Breeze总是重写构造函数字段(在我的KO示例中),因此定义为
[]
是有效的。但不确定当属性是预定义的时为什么会发生这种情况

有人碰到过这个,或者我在什么地方漏掉了一张医生的便条吗

我们来看看

是的,Breeze假设在构造函数中添加的每个属性都应该按照流行的“模型库”重写,在您的例子中,它是KO。因此,阵列成为ko.array也就不足为奇了

此外,因为这样一个属性被假定在Breeze的管辖范围内,所以我们必须将其绑定到Breeze可观测性和序列化机制中,这意味着我们将其重新编写为一个Breeze风格的可观测数组。这样的数组是一个计算的数组

显然,对于“未映射”的属性,我们这样做的方式存在一些问题。我们来看看

注意:我假设(并且您的代码确认)数组属性
extendedArray
,在某种意义上是一个“未映射属性”。那应该很好

在构造函数中不应提及映射的集合导航属性。我想不出这样做的正当理由。在构造函数中提及映射属性的主要原因是(a)为其提供默认值或(b)使其可用于自定义(未映射)计算属性。集合导航属性没有合理的可选默认值(默认值为空),将其包含在计算属性中是罕见的/可以避免的。

我们将研究它

是的,Breeze假设在构造函数中添加的每个属性都应该按照流行的“模型库”重写,在您的例子中,它是KO。因此,阵列成为ko.array也就不足为奇了

此外,因为这样一个属性被假定在Breeze的管辖范围内,所以我们必须将其绑定到Breeze可观测性和序列化机制中,这意味着我们将其重新编写为一个Breeze风格的可观测数组。这样的数组是一个计算的数组

显然,对于“未映射”的属性,我们这样做的方式存在一些问题。我们来看看

注意:我假设(并且您的代码确认)数组属性
extendedArray
,在某种意义上是一个“未映射属性”。那应该很好


在构造函数中不应提及映射的集合导航属性。我想不出这样做的正当理由。在构造函数中提及映射属性的主要原因是(a)为其提供默认值或(b)使其可用于自定义(未映射)计算属性。集合导航属性没有合理的可选默认值(默认值为空),并且很少/可以避免将其包含在计算属性中。

假设是正确的,
extendedArray
在某种意义上显示为
isunmapping:true
。关于这些扩展属性的任何内容都不是用来导航的——只用于客户端特定的数据。我正试图利用breeze导出/导入来实现客户端逻辑删除功能——因此,我希望在ctor中定义,而不是通过初始值设定项定义。感谢您的关注……这些信息很有帮助。听起来像是一个潜在的微风虫。我们将看到假设是正确的,
extendedArray
在某种意义上显示为
isunmapping:true
。关于这些扩展属性的任何内容都不是用来导航的——只用于客户端特定的数据。我正试图利用breeze导出/导入来实现客户端逻辑删除功能——因此,我希望在ctor中定义,而不是通过初始值设定项定义。感谢您的关注……这些信息很有帮助。听起来像是一个潜在的微风虫。我们会看的