2014-11-18 2 views
2

localstorage 어댑터와 emnber-data를 사용하고 hasMany 관계가있는 레코드를 삭제할 때 해당 레코드를 모두 파괴하려고합니다.Emberjs, ember-data, localstorage 어댑터, destroyRecord 및 모든 hasMany 관계

다음은 모델입니다 :

App.Category = DS.Model.extend({ 
    title: DS.attr('string'), 
    items: DS.hasMany('item') 
}); 

App.Item = DS.Model.extend({ 
    name: DS.attr('string'), 
    category: DS.belongsTo('category') 
}); 

내가 상관없이 내가 단지 불확실성을 얻을 무엇을하려고 몇 가지를 시도하지 적이 있지만있는 'destroyCategory'라는 조치를해야합니다.

나는 "아이들"에 대해 루핑을 시도했지만, 나는 단지 "부모"를 파괴하려고 시도했지만 아무도 예상대로 작동하지 않습니다. 내가 뭔가를 놓친 것 같아 도움을 주셔서 감사합니다.

편집 :

OK, 나는이 시도했다 :

deleteItem: function(user_cat) { 
this.store.find('category', user_cat.id).then(function(category) { 
    items = category.get('items'); 
    items.forEach(function(item, index, enumerable) { 
    item.destroyRecord(); 
    }); 
    category.destroyRecord(); 
}); 

내가 얻을 불확실성이 하나의 "아이"레코드 ... 와트를 제외하고 모두 파괴이다?

LAST UPDATE :

이 작동하지만 더 나은, 더 관용적 접근 방식이 궁금 :

deleteItem: function(selection) { 
this.store.find('category', selection.id).then(function(category) { 
    items = category.get('items'); 
    items.toArray().forEach(function(item, index, enumerable) { 
    item.destroyRecord(); 
    }); 
    category.destroyRecord(); 
}); 
} 
+0

을 무엇입니까 달성하려고? 나는 당신이 이미'store /'에있는 항목을 현재'컨트롤러/모델'에서 제거한다고 가정합니다. – tr3online

+0

사용자가 상위 카테고리를 파괴하면 그 안에있는 모든 항목도 파괴되어야합니다. – jasongonzales

+0

이것들은'store'에 이미 저장되어 있습니까? 즉 :'category.save()'를 수행했는지 또는 모두 미리 저장 했습니까? – tr3online

답변

1

당신은 항목에 대한 invoke('destroyRecord') 수 있습니다

deleteItem: function(selection) { 
this.store.find('category', selection.id).then(category => { 
    let items = category.get('items'); 
    items.toArray().invoke('destroyRecord'); 
    category.destroyRecord(); 
}); 
}