정말 내 머리를 감쌀 수 없습니다. 어떤 이유인지 (그리고 비동기 요청과 관련하여 무언가가) toJSON() 함수를 사용하면 내 모델 속성이 제대로 변환되지 않습니다. FWIW, 나는 django-relational fetchRelated()로 게으른 로딩을 시도하고있다.Backbone.js toJSON()은 속성을 포함하지 않습니다.
여기 fetchRelated() 요청을 작성하고 새보기를 작성합니다.
render: function(){
console.log("movies to JSONify", this.model.attributes);
console.log("movies JSONified", this.model.toJSON());
$(this.el).html(this.template(this.model.toJSON()));
return this;
여기 키커의 : 다음 UserMovieListItem 기능을 렌더링 호출
render: function(){
$(this.el).html(this.template({}));
var usermodel = this.model;
var wrapper = $(".movies");
var recipes = usermodel.get("movies");
movies.each(function(movie){
var movie_item = new UserMovieListItem({
model:movie
});
movie_item.render();
wrapper.append(movie_item.el);
});
return this;
:
$.when(usermodel.fetchRelated("movies")).then(function(){
var newview = new UserMovieList({model: usermodel});
newview.render();
});
는 그리고 이것은 렌더링 다음 함수를 호출합니다. 첫 번째 console.log (this.model.attributes)는 필요한 모든 속성을 표시합니다. 그것은 완벽하게 잘 보인다. 하지만 (this.model.toJSON())은 fetchRelated() 전에 제공되었던 URI와 id 속성 만 반환합니다. 도대체 무슨 일이 벌어지고있는거야? 내가 렌더링하는 '변화'와 UserMovieListItem에 this.model을 결합하면, 그것은 렌더링하지만 것입니다 후 0.5 초 정도 ... 가},
내 사용자 모델과 같은 정의를 :
window.User = Backbone.RelationalModel.extend({
urlRoot: '/movie/api/v1/users/',
relations: [{
type: Backbone.HasMany,
key: 'movies',
relatedModel: 'Movie',
collectionType: 'UserMovieCollection',
reverseRelation: {
key: 'user',
includeInJSON: 'id'
}
}],
select: function(){
var model = this;
model.set({selected: true});
}
});
당신이 ("조리법 usermodel.fetchRelated'에 대한 usermodel' 및 JSON 응답 '에 대한 정의를 추가 할 수 ")''$ .when (usermodel.fetchRelated ("recipes "))'가 모델 속성을 설정하고 있는지 확인하십시오. – Deeptechtons
방금 입력 오류가 있음을 깨달았습니다. 그러나 이것은 코드에 영향을 미치지 않습니다. 나는 내 질문도 업데이트 할 것이다. – bento
그리고 .then (function() {})에서 usermodel의 console.log를 실행하면; 속성이 관련 영화 모델에 적용되고 업데이트되었음을 보여줍니다. – bento