我有以下这个模型:
#order/model.coffee
Order = DS.Model.extend {
line_items: DS.hasMany 'product', {async: true}
}
在某些时候,我想向订单中添加一些产品。我发现我只能添加一次产品,再次添加相同的产品无效:
#product/route.coffee
...
actions:
# Not actually my code but illustrates the problem
addToCart: (product1, product2)->
order = @modelFor 'order'
console.log order.get('line_items.length') # prints 0
order.get('line_items').pushObject product1
console.log order.get('line_items.length') # prints 1
order.get('line_items').pushObject product2
console.log order.get('line_items.length') # prints 2
order.get('line_items').pushObject product1
console.log order.get('line_items.length') # prints 2
order.get('line_items').pushObject product2
console.log order.get('line_items.length') # prints 2
...
问题在于用户可能想要多次获取单个项目。最简单的表示方法是使用带有重复条目的数组。似乎Ember不允许我在关系中这样做。如何使一个模型多次添加到关系中?