2012-07-11 2 views
1

Backbone.Marionette의 CompositeView를 사용하고 있는데 컬렉션의 인덱스 0에 새 모델을 삽입하여 컴포지트의 첫 번째보기로 표시하려고합니다.Backbone.marionette compositeview가 addItemView의 인덱스를 무시합니다.

PlansApp.Plans.collection.unshift new PlansApp.Plan data 

문제는의 CompositeView 컬렉션에 새로 삽입 된 항목의 인덱스를 무시한다는 것입니다 :

나는 요소를 삽입하는이 코드가 있습니다. Backbone.Marionette에서

는 appendHtml 방법은 다음과 같습니다

appendHtml: function(collectionView, itemView, index){ 
    collectionView.$el.append(itemView.el); 
    }, 

인덱스 인수가 사용되지 않습니다.

새로 삽입 된보기는 항상 컴포지트의 끝에 배치됩니다.

어떻게 0 위치에 나타나게 할 수 있습니까?

답변

3

이것은 정렬 된 컬렉션을 직접 지원할 때 확인 된 가장자리의 수가 많기 때문에 의도적으로 설계된 것입니다.

index 매개 변수가 추가되어 개발자가 응용 프로그램에서 개별적으로 지원을 추가 할 수 있습니다. https://github.com/derickbailey/backbone.marionette/wiki/Adding-support-for-sorted-collections


MySortedView = Backbone.Marionette.CompositeView.extend({ 
    // ..., 

    appendHtml: function(collectionView, itemView, index){ 
    var childrenContainer = $(collectionView.childrenContainer || collectionView.el); 
    var children = childrenContainer.children(); 
    if (children.size() === index) { 
     childrenContainer.append(itemView.el); 
    } else { 
     childrenContainer.children().eq(index).before(itemView.el); 
    } 
    } 

}); 
+0

: https://github.com/marionettejs/backbone.marionette/wiki/Adding-support-for-sorted- 컬렉션 –

0

원래 Derick의 대답은 마리오네트의 현재 버전 일자 비트, 그리고 위키는 collectionview 만 예를 포함이이 작업을 수행하는 방법의 예를 보여줍니다 위키 페이지입니다. 나는 현재 compositeviews에 사용하는 것을 여기

가있다 :

위키 페이지가 실제로 여기에 있습니다
appendHtml: (collectionView, itemView, index) -> 
    $container = @getItemViewContainer(collectionView) 
    if index is 0 
    $container.prepend itemView.el 
    else 
    childAtIndex = $container.children().eq(index) 
    if childAtIndex.length 
     childAtIndex.before itemView.el 
    else 
     $container.append itemView.el 
관련 문제