Ember.js array as model's property

风格不统一 提交于 2020-01-01 01:32:09

问题


Cheers! I have some model, and one attribute of it is an array, but for some reasons (I use mongoDB on the server and it's problem with embedded models and ember-data) I can't do somthing like this:

App.Foo = DS.Model.extend({
  ...
  numbers: DS.hasMany('App.Bar')
)};

App.Bar = DS.Model.extend({
  ...
  number: DS.attr('number')
});

I need something like this:

App.Bar = DS.Model.extend({
  numbers: DS.attr('array')
});

But there is no array type of attributes in ember-data, how to be?


回答1:


I found that actually you can have array properties out of the box by just not specifying a type.

#coffeescript

AskuWhiteLabel.SomeModel = DS.Model.extend
    some_ids: DS.attr()

I'm using this, and when I do this

myModel.set('some_ids', [1,2,3])
myModel.save()

The payload to the server is indeed my array as is.




回答2:


For those, who have the same problem as me: check out this answer:

https://stackoverflow.com/a/13884238/1662820

Or you can pass embedded models with hasMany relation and set custom primary key for embedded model in the adapter ('number' in my case). Look at this tests:

https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/embedded/embedded_without_ids_test.js




回答3:


anArrayAttr: DS.attr('raw', { defaultValue: function() { return []; } })

From my awesome colleague 'Theron Humiston'



来源:https://stackoverflow.com/questions/14879013/ember-js-array-as-models-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!