Ember.js array as model's property
20,175
Solution 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.
Solution 2
For those, who have the same problem as me: check out this answer:
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:
Solution 3
anArrayAttr: DS.attr('raw', { defaultValue: function() { return []; } })
From my awesome colleague 'Theron Humiston'
Author by
xamenrax
Updated on July 05, 2022Comments
-
xamenrax almost 2 years
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?
-
xamenrax over 9 yearsCheers, Andy, I asked this question on Feb 14 '13, so it's it could be outdated or smth.
-
andy over 9 years@nikita no problem! Yeah I didn't find any documentation around it, I think I just tried it out. Hope it helps!
-
inquisitive about 9 yearsYeah it helps me though :D
-
Ember Freak over 6 yearswhat is
raw
? did you define custom transform? -
Epirocks almost 4 yearsDS.attr() on its own was enough