Javascript 主干模型 .toJSON() 不会将所有属性呈现为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10262498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone model .toJSON() doesn't render all attributes to JSON
提问by egidra
I need to render a model's attributes to JSON so I can pass them into a template. Here is what a render() function for a view looks like:
我需要将模型的属性呈现为 JSON,以便我可以将它们传递到模板中。这是视图的 render() 函数的样子:
render: function() {
console.log(this.model);
console.log(this.model.toJSON());
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
Here is the attributes output after doing console.log(this.model):
这是执行 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/"
Here is the model's JSON output after doing console.log(this.model.toJSON()):
这是执行 console.log(this.model.toJSON()) 后模型的 JSON 输出:
id: "29"
__proto__: Object
What happened?
发生了什么?
Edit: Here is the instantiation:
编辑:这是实例化:
var goal = new Goal({id: id});
goal.fetch();
var goalFullView = new GoalFullView({
model: goal,
});
Here are the contents of the new view:
以下是新视图的内容:
console.log(this.model.attributes);
console.log(this.model.toJSON());
Here is what the console says:
这是控制台所说的:
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
If the toJSON is supposed to make a clone of the attributes, why doesn't it copy the correct name or why doesn't it copy the created_at, updated_at fields?
如果 toJSON 应该复制属性,为什么不复制正确的名称或为什么不复制 created_at、updated_at 字段?
Edit 2: Here is the model:
编辑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,
},
});
Edit 3: I figured out that I need to use the success callback from fetch to render a view that uses the model:
编辑 3:我发现我需要使用 fetch 的成功回调来呈现使用该模型的视图:
goal.fetch({success: function(model) { var goalFullView = new GoalFullView({ model: goal, }); }});
goal.fetch({success: function(model) { var goalFullView = new GoalFullView({ model:goal, }); }});
回答by jimr
The toJSON()
method just returns a shallow clone of the model's attributes
property.
该toJSON()
方法仅返回模型attributes
属性的浅层克隆。
From the annotated Backbone.js source:
toJSON: function(options) {
return _.clone(this.attributes);
}
Without seeing more of your code, it looks like you directly set properties on the model object, rather than using the set
function to set model attributes.
在没有看到更多代码的情况下,看起来您直接在模型对象上设置属性,而不是使用set
函数来设置模型属性。
I.e. don't do this:
即不要这样做:
model.name = "item";
do this:
做这个:
model.set("name", "item");
EDIT:
编辑:
For your specific issue, it's possible that you called toJSON before the model had finished loading from the server.
对于您的特定问题,您可能在模型完成从服务器加载之前调用了 toJSON。
E.g. This won't always work as expected:
例如,这并不总是按预期工作:
var model = new Goal({id: 123});
model.fetch();
console.log(model.toJSON());
But this will:
但这将:
var model = new Goal({id: 123});
model.fetch({
success: function() {
console.log(model.toJSON());
}
});