简体   繁体   中英

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

I'm using DS.RESTAdapter, php on server. I have a model UacList:

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

Ember sends request 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). No matter if there is a quotes ("selected":"32483") or not ("selected":32483), it is undefined anyway. 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?

Just reading the docs here: 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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