我正在为登录用户设置"saved item"功能 . 我已经modeling worked out(它将保存的项目列表与用户和产品相关联) . 但我在如何在我的 saveditem
模型上拥有计算属性时遇到问题 . 该模型:
// saveditem.js model
export default DS.Model.extend({
user: belongsTo('user'),
product: belongsTo('product'),
dateAdded: attr('string')
});
我目前正在使用产品ID作为该模型的 id
.
我需要该模型的计算属性,因为无论何时在网站上显示产品,UI都需要反映该项是否已经存在于 saveditem
ember-data存储中 .
如果项目不在列表中(但可以添加)的示例:
列表中的项目示例(但可以删除):
我想在我的 userprofile
服务上管理用户在应用程序中的数据,我可以有一个计算属性,从 saveditem
模型中输出一组id:
savedItemsList: computed('WHAT.IS.DEPENDENT.KEY?.[]', function() {
return this.get('store').peekAll('saveditem').map(item => item.id);
}),
然后在模板中我可以使用可组合帮助器来查看正在显示的项目是否在列表中:
{{#if (contains product.id userprofile.savedItemsList)}}
...show already-saved button...
{{else}}
...show save button...
{{/if}}
The question: what would the dependent key be for this computed property? OR...is this method stinky and and there's a better way to do it?
我试过了:
savedItemsList: computed('store.saveditem.[]', function() {
(当然注入商店后) . 看起来很明显,但是当从 saveditem
商店添加或删除记录时它不会更新 . 还尝试了 'store.saveditems.[]'
,有和没有数组括号的排列,以及没有依赖键 - 都没有用 .
让我想知道这是否有可能是有充分理由的;)我可能在这里“打架” . 任何帮助赞赏!
1 回答
您可以引入计算属性,该属性将返回商店中的所有商品,并将该属性用于
savedItemsList
computed属性 .