Backbone.js的解析1元素(标识) [英] backbone.js parse 1 element (the Id)

查看:91
本文介绍了Backbone.js的解析1元素(标识)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关骨干模型中的ID,它只是 ID 以及所有更低套管。如果我的服务器上的ID是叫用户ID 。在骨干网的解析方法,我怎么改用户ID ID 并使用相同的名称为所有其他属性?

For a id on a model in backbone, its just id and all lower cased. What if my Id on the server is called UserId. In the parse method for backbone, how do I change UserId to id and use the same names for all other properties?

有关如。

window.User = Backbone.Model.extend({
    defaults:
       {
           UserId: 0, // <--can i just tell backbone that this is my id?
           Name: '',
           Age: 0
       }
    parse: function(response){
           var model = response;
           model.id = response.UserId;
           return model;
       }
});

有没有更好的方法来做到这一点?

Is there a better way to do this?

如何告诉骨干模式,我的id是类型用户ID

How about telling backbone model that my id is of type UserId.

推荐答案

做到这一点,像这样:

parse: function(response) {
  var attrs = {};
  attrs.id = response.UserId;
  return attrs;
}

解析有责任返回一个属性哈希,而不是一个模式。因此,您只需要变换反应成散列的属性与一个模型,你正在做的。

Parse has the responsibility to return an attributes hash, not a model. As such, you need only transform the response into an attributes hash versus a model as you are doing.

这篇关于Backbone.js的解析1元素(标识)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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