2012-11-28 2 views
3

나는 비동기 적으로 파일을 업로드하고와 파일 목록에서 특정 인덱스 (FormData를 사용하여) :컬렉션에서 생성 된 백본 모델의 올바른 순서를 보장하는 방법은 무엇입니까?

files.create({position: index + 1 }, {at: index}); // files is Backbone.Collection 
서버는 다음 업로드를 저장하고 삽입 새를위한 장소를 확보하기 위해 특정 위치 후 파일의 위치를 ​​이동

파일.

클라이언트에서 이벤트 추가 및 옵션에서 색인을 사용하여 파일보기를 삽입합니다.

files.on("add", function(model, collection, options) { 
    // insert file view at options.index position 
}) 

나는 또한 컬렉션의 모든 모델에 대한 position 특성을 업데이트 :

files.on("add remove", function(){ 
    this.each(function(m, index) { 
    m.set({position: index + 1}); 
    }) 
}); 

을 문제는 내가 같은 인덱스 위치에서 한 번에 여러 파일을 업로드 할 때입니다, 파일보기는의 목록에 추가 잘못된 순서.

올바른 순서와 백본 컬렉션의 파일 모델에 대한 position 속성을 보장하는 방법은 무엇입니까? 대신 자바 스크립트 유래 위치를 사용

+1

모음을 'position'으로 정렬하여 유지하려면 [비교기] (http://backbonejs.org/#Collection-comparator)를 사용할 수 없습니까? –

+0

컬렉션에 새 모델을 삽입하면 나머지 모델의 위치가 서버에서 변경됩니다 (클라이언트에는이 정보가 없습니다). –

+0

그리고 드래그 앤 드롭 정렬 작업을 위해 서버와 동기화 상태를 유지해야합니다. –

답변

0

대기열 아약스 요청을 처리하는 collection.create를 재정의 할 수있었습니다. 그렇게하려면 this answer의 코드를 사용했습니다.

var Documents = Backbone.Collection.extend({ 

    model: Document, 

    url: "https://stackoverflow.com/a/documents", 

    initialize: function() { 
    this.on("add remove", this.updatePositions, this); 
    this.ajaxQueue = $({}); 
    }, 

    updatePositions: function(model, collection, options) { 
    this.each(function(m, index) { 
     m.set({position: index + 1}); 
    }) 
    }, 

    create: function(model, options) { 
    var jqXHR 
     , dfd = $.Deferred() 
     , promise = dfd.promise() 
     , coll = this; 

    // queue our ajax request 
    this.ajaxQueue.queue(doRequest); 

    // add the abort method 
    promise.abort = function(statusText) { 
     // Proxy abort to the jqXHR if it is active 
     if (jqXHR) { 
     return jqXHR.abort(statusText); 
     } 

     // If there wasn't already a jqXHR we need to remove from queue 
     var queue = this.ajaxQueue.queue() 
     , index = $.inArray(doRequest, queue); 

     if (index > -1) { 
     queue.splice(index, 1); 
     } 

     // Reject the deferred 
     dfd.rejectWith(options.context || options, 
        [promise, statusText, ""]); 

     return promise; 
    }; 

    // Run the actual collection.create 
    function doRequest(next) { 
     options = options ? _.clone(options) : {}; 
     model = coll._prepareModel(model, options); 
     if (!model) return false; 
     if (!options.wait) coll.add(model, options); 
     var success = options.success; 
     options.success = function(nextModel, resp, xhr) { 
     if (options.wait) coll.add(nextModel, options); 
     if (success) { 
      success(nextModel, resp); 
     } else { 
      nextModel.trigger('sync', model, resp, options); 
     } 
     }; 
     jqXHR = model.save(null, options) 
     .done(dfd.resolve) 
     .fail(dfd.reject) 
     .then(next, next); 
    }; 

    return promise; 
    } 

}); 
0

는 사용 서버는 예를 들어 유닉스 타임 스탬프를 들어, ID를 생성하고, MU가 너무 짧은 제안으로 당신은 항상 당신을 수 정렬 더 구체적인 필요하면, 일종의 컬렉션은

comparitor: function() { 
    return -this.timestamp // negative ensures latest items show up on top, and olders at the bottom 
} 

를 사용하여 에테르는 더 세분화 된 미세 시간으로 가거나 단순히 증분 ID 서버 측을 생성합니다.

+0

시간 순서가 필요하지 않습니다. 그것은 정렬 키로 자바 스크립트 위치와 함께 작동합니다. 그런 다음 서버가 위치를 업데이트하여 점진적인지 확인합니다. 그러나 각 파일이 비동기 적으로 업로드되므로 파일이 잘못된 순서로 삽입됩니다. 나는 Ajax 요청을 대기열에 넣기 위해 collection.create를 오버라이드 할 수 있었다. 작동하는 것 같습니다. –

관련 문제