2013-07-15 72 views
1

在導入過程中遇到由ko.observableArray()屬性擴展的實體時出錯,而在構造函數中被擴展爲簡單數組[]類型。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 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 
    }; 

我創建了一個測試沿側微風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'); 
}); 

em2.importEntities(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),定義爲[]作品。但不知道爲什麼這種情況發生時,該屬性是預先定義的?

任何人都會遇到這種情況,或者我錯過了某處的文檔說明嗎?

回答

1

我們來看看吧。

是的,Breeze假定在構造函數中添加的每個屬性都應該根據流行的「模型庫」重寫,在您的情況下,它是KO。因此,數組變成ko.observableArray並不奇怪。此外,由於這種屬性被認爲是在Breeze的管轄範圍之內,所以我們必須將其與Breeze的可觀察性和序列化機制聯繫起來,這意味着我們將它重新編寫爲Breeze風味的可觀察數組。這樣一個數組是一個計算。

很明顯,我們對於「未映射」屬性的做法存在一些問題。我們會看看。

N.B:我假設(和你的代碼確認),該數組屬性,extendedArray,是一個「未映射屬性」,在微風的感覺。這應該沒問題。

您不應該在構造函數中提及映射的集合導航屬性。沒有正當理由可以這樣做,我可以想到。在構造函數中提及映射屬性的主要原因是(a)給它一個默認值或(b)使它可用於一個自定義(未映射)的計算屬性。集合導航屬性沒有合理的替代默認值(空是默認值),將其包含在計算中是很少/可避免的。

+0

假設是正確的,'extendedArray'在Breeze意義上顯示爲'isUnmapped:true'。對這些擴展屬性沒有任何意圖進行導航 - 僅限於客戶端特定數據。我試圖利用微風導出/導入客戶端邏輯函數 - 並且因此想要通過初始化器在ctor中定義。謝謝你看看... –

+0

這一點的信息幫助。聽起來像是一個潛在的Breeze錯誤。我們會看 – Ward