콜렉션에서 위드 문제가 발생합니다. 먼저 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;
});
"내 컬렉션이 두 번 호출 된 것 같습니다 (내 itemviews가 두 번 발생했습니다)." 좀 더 자세히 설명해주세요.이 코드에서는 itemView가 전혀 호출되지 않은 것을 볼 수 있습니다. PortraitsCompositeView를 초기화하는 무언가에 연결될 때마다 컬렉션이 한 번만 가져 오거나 처음 갈 때 가져 오게된다는 것을 의미합니까? 라우터의 코드가 도움이 될 것입니다. – RustyToms
PortraitCollection을 싱글 톤으로 만들려고 했습니까? 'Class'를 반환하는 대신 새로운 PortraitCollection을 반환하겠다는 결정은 "나쁜 냄새"입니다 !!! – ekeren