我实在无法环绕这一个任务。 出于某种原因, (我怀疑它与非同步请求有关) 我的模型属性没有使用 TOJSON () 函数进行正确的转换。 FWWW, 我试图用 django- relational relational relatet related () 进行懒惰的装入 。
s 在这里我提出了freetlecont() 请求并创建了新视图 。
$.when(usermodel.fetchRelated("movies")).then(function(){
var newview = new UserMovieList({model: usermodel});
newview.render();
});
这就要求发挥以下作用:
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;
然后称为用户MovieListItem 产生功能 :
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;
Here s the kicker. The first console.log (this.model.attributes) displays all of the attributes as they should be. It seems perfectly fine. But (this.model.toJSON()) only returns the URI and id attributes which were provided before the fetchRelated(). What the heck is going on here? If I bind this.model in UserMovieListItem with change to render, then it will render, but after half a second or so... },
我的用户模式定义如下:
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});
}
});