javascript Ember.js 数组作为模型的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14879013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ember.js array as model's property
提问by xamenrax
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:
干杯! 我有一些模型,它的一个属性是一个数组,但由于某些原因(我在服务器上使用 mongoDB,这是嵌入式模型和 ember-data 的问题)我不能做这样的事情:
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?
但是ember-data里面没有array类型的属性,怎么会呢?
回答by andy
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.
服务器的有效负载确实是我的数组。
回答by xamenrax
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:
或者,您可以传递具有 hasMany 关系的嵌入式模型,并为适配器中的嵌入式模型设置自定义主键(在我的情况下为“数字”)。看看这个测试:
回答by Naveen N
anArrayAttr: DS.attr('raw', { defaultValue: function() { return []; } })
From my awesome colleague 'Theron Humiston'
来自我很棒的同事“Theron Humiston”