백본 js를 사용하여 모음집을 만들려고 시도 할 때 isssue로 실행 중입니다. 여기서 코드 :백본 js 모음집 발행
모델 및 컬렉션은 : 플레이리스트 수집
var Track = Backbone.Model.extend({
defaults : {
title : ""
}
})
var TrackCollection = Backbone.Collection.extend({
model : Track,
})
var Playlist = Backbone.Model.extend({
defaults : {
name : "",
tracks : new TrackCollection,
}
})
var PlaylistCollection = Backbone.Collection.extend({
model : Playlist,
})
생성 : 여기
var playlists = new PlaylistCollection;
// create and push the first playlist
playlists.push({ name : "classic" });
// create and push a track in the playlist just created
playlists.last().get("tracks").push({ title : "fur elise" });
// create and push the second playlist
playlists.push({ name : "c2c" });
// create and push a track in the playlist just created
playlists.last().get("tracks").push({ title : "fuya" });
// display first playlist
console.log(JSON.stringify(playlists.at(0).toJSON()))
// display second playlist
console.log(JSON.stringify(playlists.at(1).toJSON()))
출력이다
{"name":"classic","tracks":[{"title":"fur elise"},{"title":"fuya"}]}
{"name":"c2c","tracks":[{"title":"fur elise"},{"title":"fuya"}]}
문제는, 출력에서 볼 수 있듯이 2 개의 재생 목록에는 2 개의 트랙 "모피 elise"와 "fuy 에이".
제 질문은 왜입니까? "c2c"라는 두 번째 재생 목록에서만 "classic"및 "fuya"라는 첫 번째 재생 목록에서만 "fur elise"를 가지려면 어떻게해야합니까?
감사합니다. 새 인스턴스를 만들 때
var Playlist = Backbone.Model.extend({
defaults : {
name : "",
tracks : new TrackCollection,
}
});
등뼈가 defaults
을 얕은 복사합니다 :
당신은 완전히 옳았습니다. 인스턴스화시 기본값의 복사본을 만들고 있다고 생각했습니다. 어쨌든, 귀하의 답변에 많은 감사드립니다! – Kraoz
이것은 훌륭한 정보이며 매우 적절하지만 초기화 메소드에서 새 콜렉션을 작성하는 것이 더 쉽고 명확 할 수 있습니까? –
@AlexMills : 아마도.나는 그들의 상황에 대해 충분히 알지 못했다. 이 답변은 "만약 당신이 이것을 할 것이면, 그러면 그렇게 할 것"이라고 생각합니다. –