[英]get() is returning arrays as non-arrays
I have this model: 我有这个模型:
let model = new falcor.Model({
cache: {
hStudents: {
801: {
FirstName: 'Samuel',
LastName: 'Forbes'
},
802: {
FirstName: 'Chad',
LastName: 'Pennington'
}
},
lStudents: [
$ref(['hStudents', 801]),
$ref(['hStudents', 802])
]
}
});
When I execute the following code, although lStudents is an array in the model, I'm getting back an object which is not an array. 当我执行以下代码时,尽管lStudents是模型中的数组,但我正在获取不是数组的对象。 For that reason, I cannot use Angular's *ngFor to iterate over the object.
因此,我无法使用Angular的* ngFor来迭代对象。 Why would I not get an array back?
为什么我不找回阵列?
return this.model.get("lStudents[0..1]['FirstName','LastName']")
.progressively()
.subscribe( (h) => {
console.log(JSON.stringify(h));
});
prints: 打印:
{"json":{
"lStudents":{
"0":{"$__path":["hStudents","801"],"FirstName":"Samuel","LastName":"Forbes"},
"1":{"$__path":["hStudents","802"],"FirstName":"Chad","LastName":"Pennington"},"$__path":["lStudents"]}
}
}
Falcor treats arrays as objects with keys equal to the array item's index. Falcor将数组视为对象,其键等于数组项的索引。 See the documentation example here .
请参阅此处的文档示例。
The reason for this is that because the lStudents
list is paginated, it will have to handle queries like "lStudents[10..19]['FirstName','LastName']"
. 这样做的原因是,因为
lStudents
列表是分页的,所以它必须处理诸如"lStudents[10..19]['FirstName','LastName']"
。 If the result were to be an array rather than object, eg { "json": "lStudents": [ <items 10 - 19> ] }
, it would conflict with a similar query asking for students 0 - 9 (or any other student list query). 如果结果是数组而不是对象,例如
{ "json": "lStudents": [ <items 10 - 19> ] }
,则它将与类似的查询要求学生0-9(或任何其他学生)冲突列表查询)。
To handle objects representing lists, convert the jsonGraph response to an array. 要处理表示列表的对象,请将jsonGraph响应转换为数组。 A utility library like lodash or ramda might come in handy.
像lodash或ramda这样的实用程序库可能会派上用场。
this.model.get("lStudents[0..1]['FirstName','LastName']")
.progressively()
.subscribe((jsonEnvelope) => {
/* using Ramda and transforming
* { "FirstName":"Samuel","LastName":"Forbes" }
* to { "FirstName":"Samuel","LastName":"Forbes", "index": "0" }
*/
console.log(
R.compose(
R.map(([studentIdx, student]) => ({
...student,
index: studentIdx
})),
R.toPairs
)(jsonEnvelope.json.lStudents);
);
/* using Ramda and ignoring index
*/
console.log(
R.values(jsonEnvelope.json.lStudents)
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.