【发布时间】:2020-12-10 20:57:15
【问题描述】:
所以我的 Ember 应用程序中有一个计算属性。声明为myComProp: computed('itemTrait', function () {...})。 myComProp 属于一个项目模型。在一个单独的组件 (listItem) 中,我有一个属性是 myComProp 的别名:myAlias: alias('itemModel.myComProp')。现在 listItems 是 listItems 数组的成员,其属性在 Web 应用程序中呈现。
现在由于属性是别名,有没有办法强制计算的属性重新计算?
我尝试将别名设为跟踪属性,但这样做会将调用堆栈作为字符串返回,而不是计算函数的实际结果。
更新
添加这个以更好地解释。
// itemModel.js
export default parentModel.extend({
//irrelevant properties
myComProp: computed('itemTrait', function() {
// logic
})
});
在其他地方有两个组件。一个是呈现 itemModel 列表的页面部分,另一个是 itemModel 的表示(由列表组件从数据存储中获取)
// itemModelRep/component.js
export default Component.extend({
// other properties
myAlias('item.myComProp')
// itemModel is represented in the list by 'item'
});
还有名单:
//itemList.js
export default parentDisplay.extend({
// other properties
get items() {
// this is what is used as the list items
// it basically gets a list of items from the itemModel record set in
// the data store
listOfItems.forEach((item) => {
// Other stuff going on
// Here I want to force the property to recompute
};
}
});
基本上 myComProp 正在获取相关日期,当我用新项目更新列表时,我想重新计算相关日期。获取事件基于跟踪列表,因此每次将实时提要添加到时都会运行
【问题讨论】:
-
在你的代码中有很多无效的语法。如果使用 pre-Octane
EmberObject语法,则不能使用本机 getter (get items() {})。myAlias('item.myComProp')似乎不是有效的语法,并且该函数调用的返回值不与任何属性相关联。您能否更新以修复这些问题,以便更容易理解?在 Ember Twiddle 上进行复制也会很有帮助。 -
@jelhan 那是因为它是示例代码来传达这个想法,而不是从我们的代码库中复制片段
标签: javascript ember.js alias computed-properties