breezejs: getEntityByKey with expand
breezejs: getEntityByKey with expand
假设我有一个Product实体列表。然后我想编辑其中一个产品,所以我将调用getEntityByKey从缓存中获取它。
然而,在编辑屏幕上,我可能会显示比网格中更多的属性,在我的情况下,我实际上需要对一些Product属性执行expand()操作。
那么,关于缓存,我该如何处理呢?如果我使用getEntityByKey,它就不会执行expand()操作。我可以执行查询,但不会利用缓存的实体(以防我以前已经编辑过产品)。
在这种情况下,我应该选择什么策略?
另一种方法:向Product
实体添加isReadyForEdit
标志。如果为false,则使用常规查询获取完整的Product
WITH EXPAND以获取相关实体。如果这是真的,那么您知道您在缓存中拥有所需的一切,而getEntityByKey
就是您所需要的一切(因为相关实体也将在缓存中)。确保在任何一种情况下都返回一个promise,以便您的调用者逻辑准备等待。这与John Papa的例子中的isPartial
是相同的技术。
12月17日更新
忘了提一下,与John Papa的例子不同,isReadyForEdit
不应该是一个未映射的属性,除非你需要序列化它的状态(即使这样,我也不太确定)。当属性为true时,只需将其添加到实体中即可。
没有说JP的所作所为是错误的;这符合他的离线场景。但是,如果您不需要Breeze感知功能
我经常使用的一个选项是创建某种类型的导航属性,然后通过对服务器的调用来实现它。这将允许您拉入实体的当前属性,然后当导航属性具体化时,它将显示出来。粗略示例-
在您的JavaScript中的某个位置(假设此处为KO,因此在视图模型中)
var thisPerson = ko.observable();
// Set this person equal to the person returned from a call from cache
thisPerson(datacontext.getPersonById(1));
// Call to the server to get this persons boss
datacontext.getPersonsBoss(thisPerson);
在视图
<div data-bind="with: thisPerson">
// Bind to properties of this person
<div data-bind="with: boss">
// Bind to the properties of a boss
// which is a navigation property of thisPerson
</div>
</div>
基本上,你从缓存中获取此人,然后从服务器中获取此人的上司。由于person
具有boss
的导航属性,因此一旦Breeze返回boss并将其放入缓存,它将自动在视图中实现。
在应用Ward的上述建议时,我注意到了一件事(对于那些同样需要此解决方案的人,如果您遇到同样的问题)。。。
运行标准查询时,请记住查询返回的data.results
是一个数组。因此,如果要返回实体本身,则需要从数组中提取它。
即成功
function succeeded(data) {
var result = data.results;
var entity = result[0];
...
return entity;
}
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- Modify Javascript with C#
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- breezejs: getEntityByKey with expand
- Expand div with "display: table-cell" on click?