以下是我问题的主要支柱:
我试图在breeze中找到一个方法(实体管理器?),其中,如果我传递从服务器检索的实体列表,这些用于更新本地缓存,但我没有找到任何。在研究了源代码之后,我在breeze库中做了一些更改,在那里我公开了MappingContext内部类并执行了以下操作(TypeScript代码):
public processEntityRequestAsync<TResponse>(request: requestModel.RequestBase): Q.Promise<TResponse> {
var promise = this.requestProcessor.processRequest<TResponse>(request);
var processedPromise = promise.then(r => {
// TODO everything in this method is a hack using internal things from breeze
// to update the entities after getting them from server
var updatedEntities = <any[]>(<any>r).UpdatedEntities;
if (updatedEntities) {
var dataService = <breeze.DataService>(<any>breeze.DataService).resolve([{
serviceName: this.manager.dataService.serviceName,
adapterName: "WebApi",
}]);
var mappingContext = new (<any>breeze).MappingContext({
query: null, // tells visitAndMerge that this is a save instead of a query
entityManager: this.manager,
mergeOptions: { mergeStrategy: breeze.MergeStrategy.OverwriteChanges },
dataService: dataService,
});
// Note that the visitAndMerge operation has been optimized so that we do not actually perform a merge if the
// the save operation did not actually return the entity - i.e. during OData and Mongo updates and deletes.
updatedEntities = mappingContext.visitAndMerge(updatedEntities, { nodeType: "root" });
}
return r;
});
return processedPromise;
}
注意:&lt; any&gt;转换用于绕过Typescript类型检查并使用breeze内部。
这是有效的,但是我不喜欢我必须使用breeze internals并且我必须为此目的修改库。有没有更好的方法来实现我的目标?
答案 0 :(得分:0)
我不完全确定我理解这个问题,但请看一下JsonResultsAdapter的讨论:http://www.breezejs.com/documentation/web-service-data-mapping
我们的想法是可以在客户端上使用 JsonResultAdapter 将任何json数据强制转换为将合并到 EntityManager 中的实体。