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

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

问题描述

我需要渲染模型对JSON属性,这样我就可以将它们传递到模板。
这里是一个什么渲染()的视图方法如下:

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后模型的JSON输出(this.model.toJSON()):

Here is the model's JSON output after doing console.log(this.model.toJSON()):

id: "29"
__proto__: Object

发生了什么事?

编辑:
这里是实例

Here is the instantiation:

  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?

编辑2:
下面是模式:

Edit 2: Here is the model:

  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:
我想通了,我需要使用成功回调从取来呈现使用模型视图:

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 =新GoalFullView({
      型号:目标,
    });
  }});

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

推荐答案

的toJSON()方法仅返回模型的属性的浅表副本属性。

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

注明来源Backbone.js的

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

在没有看到您更多的code的,它看起来像你直接设置属性的模型对象上,而不是使用设置函数来设置模型属性。

即。不这样做:

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天全站免登陆