我在 .NET 中定义了以下模型:
Public Class Request
<Key()>
Public Property RequestId As String
Public Property Description As String
Public Property InfoCards As List(Of InfoCard)
End Class
Public Class InfoCard
<Key()>
Public Property InfocardId As String
Public Property Description As String
Public Property RequestId As String
End Class
信息卡上的 ReqeuestId 属性是这样 EF Code First 可以生成数据库。然后,我使用 UpshotContext
助手生成 JavaScript 模型,如下所示:
Html.UpshotContext(bufferChanges:= true) _
.DataSource(Of UpshotTest.UpshotTest.RequestController)(Function(x) x.GetRequests()) _
.ClientMapping(Of UpshotTest.Models.Request)("Request") _
.ClientMapping(Of UpshotTest.Models.InfoCard)("InfoCard")
这很好地生成了所有模型,当我在结果数据源上调用refresh
时,数据将按预期检索。
我面临的问题是当我尝试修改子对象的属性时(例如 Request.InfoCards(0).Description
)。更改该值将触发InfoCard
中的更改,但不会在Request
实体中触发更改。因此,调用 Request.IsUpdated()
将返回 false,而调用 Request.InfoCards(0).IsUpdated()
将返回 true。
结果不知道如何跟踪子实体中的更改吗?还是客户端的映射有问题?到目前为止,我已经尝试了几种映射:
手动映射所有属性:
window.Request = function (initialData) { var self = this; self.RequestId = ko.observable(initialData.RequestId); self.Description = ko.observable(initialData.Description); self.InfoCards = ko.observableArray(ko.utils.arrayMap(initialData.InfoCards, function (data) { return new window.InfoCard(data); })); upshot.addEntityProperties(self, "Request:#UpshotTest.Models"); };
使用
upshot.map
:window.Request = function (initialData) { var self = this; upshot.map(initialData, upshot.type(Request), self); };
定义我自己的地图函数并将其传递给 Upshot:
window.Request = function (initialData) { var self = this; upshot.observability.configuration.map(initialData, upshot.type(Request), map, self); };
所有方法的映射似乎都是正确的,只是父实体不知道在其子实体中所做的修改。儿童跟踪甚至可能吗?
好的,我最终发现了问题。事实证明,当存在 3 个嵌套实体的层次结构(即请求 -> InfoCard -> InfoField)时,此行为是可重现的。修改信息字段的值未反映在父实体中。
经过几个小时的 upshot 调试,我发现罪魁祸首是 upshot.js 中的_addTrackingRecursive
方法。替换对obs.track(obj, this._getTracker(), type);
的obs.track(obj, this._getTracker(), this._entityType);
调用解决了嵌套的更改跟踪问题。