简体   繁体   English

灰烬数据:DS.attr(“ number”)应该如何工作?

[英]Ember-data: How DS.attr(“number”) is supposed to work?

I'm using DS.RESTAdapter, php on server. 我正在使用DS.RESTAdapter,服务器上的php。 I have a model UacList: 我有一个模型UacList:

DS.Model.extend({
    dummyFieldForErrors: DS.attr("string"),
    selected: DS.attr("number"),
    uacs: DS.hasMany("uac")
});

Ember sends request GET /rest/uacLists/1. Ember发送请求GET / rest / uacLists / 1。 Server response is: 服务器响应为:

{
    "uacList":{
        "id":1,
        "selected":32483,
        "uacs":[33618,9678,...,33656]
    },
    "uacs": [...]
}

But Ember Inspector tells me that selected is undefined (other attributes have values they should have according to server response). 但是Ember Inspector告诉我,selected是未定义的(其他属性具有根据服务器响应应具有的值)。 No matter if there is a quotes ("selected":"32483") or not ("selected":32483), it is undefined anyway. 无论是否使用引号(“ selected”:“ 32483”)或不使用引号(“ selected”:32483),它始终是未定义的。 If I define this attribute as a string, it works but I don't like when an integer is defined as a string. 如果我将此属性定义为字符串,则可以使用,但是我不喜欢将整数定义为字符串。

This is intended to be a comment, but my reps not good enough. 这只是一个评论,但我的代表还不够好。 I guess since you've got this working when the value's a string this isn't any issue, but don't you need to define DS in upper case? 我想,既然在值是字符串的情况下已经可以正常工作了,那么这没什么问题,但是您是否不需要以大写形式定义DS?

Just reading the docs here: http://emberjs.com/api/data/classes/DS.html#method_attr 只需在这里阅读文档: http : //emberjs.com/api/data/classes/DS.html#method_attr

By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. 默认情况下,属性按原样传递,但是您可以指定可选类型以自动转换值。

So to me this implies that you shouldn't use 'number' or 'string' for your selected attribute. 因此,对我而言,这意味着您不应为selected属性使用'number''string'

DS.Model.extend({
    dummyFieldForErrors: DS.attr("string"),
    selected: DS.attr(),
    uacs: DS.hasMany("uac")
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM