我正在尝试限制在Breeze.JS中实现Lookup Lists时遇到的数据量。 Lookup Lists示例使用导致完整对象的查询但我想将实体投影到更少的属性(例如主键,外键和描述符属性),并且仍然让Breeze.JS识别客户端上的实体类型。我知道如何从客户端进行预测以获得部分内容但是我如何使用查找列表(来自客户端或服务器Web API)来实现这一点?
答案 0 :(得分:1)
您可以使用自定义JsonResultsAdapter
来满足您的意图。
你可能想知道" 什么是JsonResultsAdapter
?"
微风用来解释从服务器到达的JSON。您可以read about here和here。
或许更有帮助的是查看the adapter in the Web API dataservice和the example adapter from the "Edmumds" sample。
Edmunds示例演示了如何将您无法控制的JSON源转换为breeze实体。
在这种情况下,你的JsonResultsAdapter
会查看JSON的每个节点并说" 这是一个Foo,这是一个Bar,那个是Baz & #34 ;.因此,对于每个节点,return { entityType: "Foo" }
,return { entityType: "Bar" }
和return { entityType: "Baz" }
现在breeze知道该怎么做,并从Lookups有效载荷中创建相应的实体。
请记住将这些实体标记为部分实体,方法与制作针对单个实体类型的投影查询的方式相同。
幸运的是,Lookups查询返回包含Foo,Bar和Baz集合的容器对象。因此,您可以迭代这些并在查询成功回调中将它们部分标记在那里。
一旦你绕过那个...你想知道如何让自定义JsonResultsAdapter
在Lookups查询中工作......并且只在Lookups查询中。
您可以将JsonResultsAdapter
专门用于查询with a using
clause。
以下是一个例子:
var jsa = new breeze.JsonResultsAdapter({
name: 'myLookupsJsa',
visitNode: function() {...}
});
query = query.using(jsa);
这有多大意义吗?你最好三次旅行吗?
只有你会知道。我想在你试用的时候听到你的消息......并告诉我们你如何以一般方式使这更容易的建议。
答案 1 :(得分:0)
在lookup lists example中,控制器操作如下所示:
[HttpGet]
public object Lookups() // returns an object, not an IQueryable
{
var regions = _contextProvider.Context.Regions;
var territories = _contextProvider.Context.Territories;
var categories = _contextProvider.Context.Categories;
return new {regions, territories, categories};
}
您可以使用服务器端投影减少占用空间,如下所示:
[HttpGet]
public object Lookups() // returns an object, not an IQueryable
{
var regions = _contextProvider.Context.Regions
.Select(x => new { id = x.RegionId, name = x.RegionName });
var territories = _contextProvider.Context.Territories
.Select(x => new { id = x.TerritoryId, name = x.TerritoryName });
var categories = _contextProvider.Context.Categories
.Select(x => new { id = x.CategoryId, name = x.CategoryName });
return new {regions, territories, categories};
}
这种方法不能回答你问题的这一部分:
并且仍然有Breeze.JS识别客户端上的实体类型
不确定这件作品的解决方案或用例是什么。