2012-12-07 5 views
1

나는 다음과 같은 모델로 채워 모음이 있습니다일괄 업데이트 수동 설정

rows: [ 
id: 1 
category: 'Meetings & Banquets' 
progress: 0.8 
, 
id: 2 
category: 'Guest Services' 
    progress: '50%' 
, 
id: 3 
category: 'Concessionaires' 
progress: '20%'  
, 
id: 4 
category: 'Telecommunications' 
progress: 100 
, 
id: 5 
category: 'Restaurant' 
progress: '70%'              
] 

내가 좋아하는 것과 같이 그 중 갱신이 일괄 및 뷰 업데이트에 결합하는 모델을 가지고 자신 :

rows: [ 
id: 1 
category: 'Meetings & Banquets' 
progress: 0.9 
, 
id: 2 
category: 'Guest Services' 
    progress: '10%' 
, 
] 

컬렉션에서 .add 또는 .reset을 수행하면 동일한 ID로 모델을 추가/업데이트 할 수 없습니다. 이 모델을 일괄 적으로 업데이트 할 수있는 옵션은 무엇입니까?

답변

2

편집 : 백본는 이제 옵션 {merge: true}으로 add 방법을 사용할 수 있습니다 0.9.9 :

addcollection.add(models, [options])

당신이 컬렉션에 이미 컬렉션에 모델을 추가하는 경우, {merge : true}를 넘기지 않는 한 무시됩니다.이 경우 속성이 해당 모델에 병합되어 적절한 "변경"이벤트가 발생합니다.


은 내가 백본 컬렉션에 지금 당신을 위해 그것을 수행하는 기능이 믿지 않는다. 어쩌면 당신은 당신 자신의 것을 만들 수 있을까요?

MyCollection = Backbone.Collection.extend({ 
    model: MyItem, 

    update: function(updatedArray) { 
    var that = this; 
    _.each(updateArray, function(element) { 
     if(that.get(element.id)) { 
     that.get(element.id).set(element); 
     } 
    }); 
    } 
}); 

다음 :

var collection = new MyCollection(rows); //creates your collection 
collection.update(rows2); //updates specific models. 
+1

이 실제로 백본의 GitHub의에 문제가 있었고 그들이 마스터 지점에서 그것을 해결했습니다 https://github.com/documentcloud/backbone/pull/1752 참조 – imrane

+0

그들이 여전히 다른 https://github.com/documentcloud/backbone/pull/1873에 대해 토론하고있는 것처럼 보이지만 모든 다른 업데이트 옵션을 사용했다면 멋질 것입니다. – hajpoj