2015-02-11 84 views
0

我有多個購物車。 每個購物車都有多個產品。Ember數據刪除嵌套記錄

當我從購物車中刪除產品時,生成的網址嘗試全局刪除產品,而不僅僅是從特定的購物車中刪除。產生

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.find("cart", 2); 
    }, 
    actions: { 
    remove: function(product){ 
     product.deleteRecord(); 
     product.save(); 
    } 
    } 
}); 

{{#each product in model.products}} 
    <div>{{product.title}} <button {{action "remove" product}}>x</button></div> 
    {{/each}} 

HTTP請求DELETE產品/ {ID}

我希望它以某種方式包括在URL車ID。如何處理這種情況?

演示:http://jsbin.com/lecejejara/1/edit?html,js

回答

3

要調用deleteRecord()其刪除記錄,從而在全球範圍中刪除。你真正需要做的是取消關聯產品和購物車。

假設在你的範圍你的車被稱爲cart,您可以更改模板,還通過購物車:

{{#each product in model.products}} 
    <div>{{product.title}} <button {{action "remove" product cart}}>x</button></div> 
{{/each}} 

並修改刪除操作撇清這個特殊的購物車和產品:

actions: { 
    remove: function(product, cart){ 
     cart.get('products').removeObject(product); 
     cart.save(); 
    } 
    } 
+0

這樣,當我想刪除一條記錄時,我必須將整個購物車發送到服務器。如何發送只有一個項目的想法被刪除,以提高性能? – Tomas 2015-02-13 09:12:00