【发布时间】:2018-04-23 23:25:43
【问题描述】:
最近这成为了我的应用程序中的一个错误,虽然我不知道发生了什么变化(我还没有升级 Ember 的版本,它仍然是 1.13)。我需要了解的是如何以常规方式访问模型上单个记录的对象。
我有以下代码根据其他两个属性过滤我的model:
recordsBySelectedShapeAndColor = get(this, "model").filter(function(rec) {
//filter the model by the chosen shape and color
return (
get(rec, "shape") === theShape &&
get(rec, "color") === theColor
);
});
然后我需要创建这些过滤记录的摘要,我正在使用 reduce(),但如果该过滤器只返回一条记录,那么 reduce 不会返回正确的结果,所以我有以下条件:
if (recordsBySelectedShapeAndColor.length < 2) {
summary = recordsBySelectedShapeAndColor[0]._data;
} else {
summary = recordsBySelectedShapeAndColor.reduce(function(a, b) {
...
}
if 中的行不再返回简单对象,因此我将其更改为 summary = recordsBySelectedShapeAndColor[0]._internalModel._data; 并且它可以工作,但它看起来很可疑(._data 总是这样做)。访问带下划线的属性是代码气味吗?如果是这样,我怎样才能只从模型上的那条记录中获取数据?
【问题讨论】: