2013-12-22 3 views
0

콜렉션에서 위드 문제가 발생합니다. 먼저 compositeView를로드하면 모든 것이 잘 작동하지만 내 앱에서 탐색을 시작한 다음 내 compositeView로 복귀 할 때 (Backbone.history.navigate) 내 컬렉션이 두 번 호출되는 것처럼 보입니다 (내 itemviews 두 번 발생).뒤로 돌아갈 때 백본 컬렉션 트리거가 두 번 발생합니다.

나는 디버깅을 시도했지만, 한 번만 내 컬렉션을 가져오고, 하나의 초기화 일뿐입니다. 라우터도 괜찮아 보입니다. 여기

'use strict'; 
define(["jquery", "backbone", "marionette", "text!templates/portraits/portrait.html", "view/portraits/portraitItemView", "collection/portraitCollection", "application", "JSMovieclip"], function($, Backbone, Marionette, template, PortraitItemView, portraitCollection, App) { 
var PortraitsCompositeView = Marionette.CompositeView.extend({ 
    template : _.template(template), 
    collection : portraitCollection, 
    tagName: "div", 
    id : "articles", 
    itemView : PortraitItemView, 
    itemViewContainer : '#list-articles', 
    itemViewOptions: { 
     collection: portraitCollection 
    }, 
    initialize : function (options) { 

     _.bindAll(this); 
     this.options = options || {}; 
     this.collection.fetch({ 
      type: 'POST', 
      success : function(data, raw) { 
      App.execute('loader', false); 
      } 
     }); 
    }, 

그리고 내 모음입니다 :

'use strict'; 
define(["jquery", "underscore", "backbone", "marionette", "model/portraitsModel"], function($, _, Backbone, Marionette, PortraitModel) { 
    var PortraitCollection = Backbone.Collection.extend({ 
     model : PortraitModel, 
     sync: function(method, model, options) { 

      var params = _.extend({ 
       type: 'GET', 
       dataType: 'jsonp', 
       url: 'http://backend.url.fr/api/portraits/get_list/', 
       processData: false 
      }, options); 

      return $.ajax(params); 
     }, 
     parse : function(response) { 
      this.totalLength = response.count; 
      return response.portraits; 
     } 
    }); 
    return new PortraitCollection; 
}); 
+0

"내 컬렉션이 두 번 호출 된 것 같습니다 (내 itemviews가 두 번 발생했습니다)." 좀 더 자세히 설명해주세요.이 코드에서는 itemView가 전혀 호출되지 않은 것을 볼 수 있습니다. PortraitsCompositeView를 초기화하는 무언가에 연결될 때마다 컬렉션이 한 번만 가져 오거나 처음 갈 때 가져 오게된다는 것을 의미합니까? 라우터의 코드가 도움이 될 것입니다. – RustyToms

+0

PortraitCollection을 싱글 톤으로 만들려고 했습니까? 'Class'를 반환하는 대신 새로운 PortraitCollection을 반환하겠다는 결정은 "나쁜 냄새"입니다 !!! – ekeren

답변

0

, 모든 것이 잘 작동하여 Collection.fetch 속성에 reset:true을 추가 할 수 있습니다.

+0

나는 똑같은 문제가있다. 빈 ID가 없다. 여전히 두 번 요청한다. –

0

귀하의 컬렉션 자체에 항목을 추가 페치가

여기 내의 CompositeView입니다.

당신은

나는 마침내 그것을 고정 후 내 오류, 내 JSON은 빈 "ID"필드를 반환 한을 발견
initialize : function (options) { 

    _.bindAll(this); 
    this.options = options || {}; 
    this.collection.fetch({ 
     reset: true, 
     type: 'POST', 
     success : function(data, raw) { 
     App.execute('loader', false); 
     } 
    }); 
}, 
+0

답변 해 주셔서 감사합니다.하지만 이미이 솔루션을 변경하지 않고 사용해 보았습니다. (나는 내 ​​첫 메시지에 내 컬렉션을 추가했다 .A. – user2283958