[英]ember: computed property on ember-data store
I am setting up a "saved item" feature for logged-in users. 我正在为登录用户设置“保存的项目”功能。 I have the modeling worked out already (it relates the saved item list to both the user and products).
我已经进行了建模 (将已保存的项目列表与用户和产品相关联)。 But I am having trouble with how to have a computed property on my
saveditem
model. 但是我在如何在我的
saveditem
模型上具有计算属性方面遇到麻烦。 The model: 该模型:
// saveditem.js model
export default DS.Model.extend({
user: belongsTo('user'),
product: belongsTo('product'),
dateAdded: attr('string')
});
I am currently using the product id as the id
of that model. 我目前正在使用产品ID作为该模型的
id
。
I need a computed property on that model because anytime a product is shown on the site, the UI needs to reflect whether the item is already in the saveditem
ember-data store or not. 我需要该模型上的计算属性,因为只要在网站上显示产品,UI都需要反映该项目是否已经在
saveditem
余烬数据存储中。
Example of if the item is not on the list (but can be added): 该项目不在列表中(但可以添加)的示例:
Example of an item that is on the list (but can be removed): 列表中一个项目的示例(但可以删除):
I was thinking that on my userprofile
service which manages the user's data across the app, I could have a computed property that outputs an array of ids from the saveditem
model: 我在想,我
userprofile
横跨应用程序管理用户的数据业务,我可以有一个计算的属性,输出来自IDS的数组saveditem
模型:
savedItemsList: computed('WHAT.IS.DEPENDENT.KEY?.[]', function() {
return this.get('store').peekAll('saveditem').map(item => item.id);
}),
And then in the template I could use a composable helper to see if the item being displayed is in the list or not: 然后在模板中,我可以使用可组合帮助器来查看所显示的项目是否在列表中:
{{#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?
或者...这种方法很臭,并且有更好的方法可以做到吗?
I have tried: 我努力了:
savedItemsList: computed('store.saveditem.[]', function() {
(after of course injecting the store). (当然是在注入商店之后)。 Seems like the obvious one but it doesn't update when records are added or removed from the
saveditem
store. 看起来很明显,但是当从
saveditem
存储中添加或删除记录时,它不会更新。 Also tried 'store.saveditems.[]'
, permutations with and without array brackets, and NO dependent key - all no worky. 还尝试了
'store.saveditems.[]'
,带有和不带有数组括号的排列以及没有从属键-都没有用。
Makes me wonder whether this is not possible for a good reason ;) I might be "fighting the framework" here. 让我怀疑是否有充分的理由无法实现;)我在这里可能是“与框架打架”。 Any help appreciated!
任何帮助表示赞赏!
You can introduce computed property which will return all the items in the store and use that property for your savedItemsList
computed property. 您可以引入计算属性,该属性将返回商店中的所有商品,并将该属性用于您的
savedItemsList
计算属性。
allSavedItemsList: computed(function() {
return this.get('store').findAll('saveditem');
}),
savedItemsList: computed('allSavedItemsList.[]',function() {
return this.get('store').peekAll('saveditem').map(item => item.id);
}),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.