主干模型 .toJSON() 不会将所有属性呈现为 JSON [英] Backbone model .toJSON() doesn't render all attributes to JSON

查看:27
本文介绍了主干模型 .toJSON() 不会将所有属性呈现为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将模型的属性呈现为 JSON,以便我可以将它们传递到模板中.视图的 render() 函数如下所示:

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:

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)后输出的属性:

Here is the attributes output after doing 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(this.model.toJSON()) 后模型的 JSON 输出:

Here is the model's JSON output after doing console.log(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 字段?

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?

这是模型:

  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, 
    },
  });

我发现我需要使用来自 fetch 的成功回调来呈现使用该模型的视图:

Edit 3: I figured out that I need to use the success callback from fetch to render a view that uses the model:

goal.fetch({成功:函数(模型){var goalFullView = new GoalFullView({模型:目标,});}});

goal.fetch({success: function(model) { var goalFullView = new GoalFullView({ model: goal, }); }});

推荐答案

toJSON() 方法只返回模型的 attributes 属性的浅层克隆.

The toJSON() method just returns a shallow clone of the model's attributes property.

来自带注释的 Backbone.js 源:

toJSON: function(options) {
  return _.clone(this.attributes);
}

没有看到你的更多代码,看起来你直接在模型对象上设置属性,而不是使用 set 函数来设置模型属性.

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.

即不要这样做:

model.name = "item";

这样做:

model.set("name", "item");

对于您的具体问题,您可能在模型完成从服务器加载之前调用了 toJSON.

For your specific issue, it's possible that you called toJSON before the model had finished loading from the server.

例如这并不总是按预期工作:

E.g. This won't always work as expected:

var model = new Goal({id: 123});
model.fetch();
console.log(model.toJSON());

但这将:

var model = new Goal({id: 123});
model.fetch({
  success: function() {
    console.log(model.toJSON());
  }
});

这篇关于主干模型 .toJSON() 不会将所有属性呈现为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆