我需要渲染模型对JSON属性,所以我可以将它们传递到模板。 这里是一个渲染什么()的视图功能是这样的:
render: function() {
console.log(this.model);
console.log(this.model.toJSON());
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
这里是干什么的console.log(this.model)后输出的属性:
created_at: "2012-04-19"
id: "29"
name: "item"
resource_uri: "/api/v1/item/29/"
updated_at: "2012-04-21"
user: "/api/v1/user/9/"
这里是干什么的console.log后模型的JSON输出(this.model.toJSON()):
id: "29"
__proto__: Object
发生了什么?
编辑:这里是实例:
var goal = new Goal({id: id});
goal.fetch();
var goalFullView = new GoalFullView({
model: goal,
});
下面是新视图的内容:
console.log(this.model.attributes);
console.log(this.model.toJSON());
这里是控制台说什么:
Object
created_at: "2012-04-23"
id: "32"
name: "test"
resource_uri: "/api/v1/goal/32/"
updated_at: "2012-04-23"
user: "/api/v1/user/9/"
__proto__: Object
Object
id: "32"
name: "test"
__proto__: Object
如果的toJSON应该使属性的克隆,为什么不把它复制正确的名称或者为什么不把它复制的created_at,的updated_at领域?
编辑2:这里是模型:
var Goal = Backbone.Model.extend({
// Default attributes for Goal
defaults: {
name: "empty goal",
},
// Check that the user entered a goal
validate: function(attrs) {
if (!attrs.name) {
return "name is blank";
}
},
// Do HTTP requests on this endpoint
url: function() {
if (this.isNew()) {
return API_URL + "goal/" + this.get("id") + FORMAT_JSON;
}
return API_URL + "goal/" + FORMAT_JSON;
//API_URL + "goal" + FORMAT_JSON,
},
});
编辑3:我想通了,我需要使用成功回调从取来呈现使用模型视图:
goal.fetch({成功:函数(模型){风险goalFullView =新GoalFullView({模型:目标,});}});