我如何为一个包含 ember 数据的普通字符串数组的对象建模?
How can I model an object that has a plain array of strings with ember data?
如何为具有普通字符串数组的对象建模?
如果我执行以下操作,它会显示电子邮件,但在修改它们的值时它们不会更新。
服务器sends/receives以下数据:
"data":
[
{
"attributes" : {
"id":1,
"name":"Jhon",
"emails":["jhon@gmail.com","jhon@other.com"]},
"relationships": (,..}
}
}
...
型号:
export default Model.extend({
id: attr('string'),
name: attr('string'),
emails: attr()
})
模板:
{{#each model.emails as |email|}}
<input type="text" value={{email}}>
{{/each}}
这就足够了:
export default Model.extend({
id: attr('string'),
name: attr('string'),
emails: attr()
})
因为电子邮件是你上面的一个列表json,所以你的对象设计最好使用一对多关系,如下所示:-
这里有 2 个对象 1]employee 2]emailList
app/models/employee.js
import DS from 'ember-data';
export default Model.extend({
id: DS.attr('string'),
name: DS.attr('string'),
emails: DS.hasMany('email-list'),
})
app/models/email-list.js
import DS from 'ember-data';
export default Model.extend({
email: DS.attr('string'),
employee: DS.belongsTo('employee')
})
如何为具有普通字符串数组的对象建模? 如果我执行以下操作,它会显示电子邮件,但在修改它们的值时它们不会更新。
服务器sends/receives以下数据:
"data":
[
{
"attributes" : {
"id":1,
"name":"Jhon",
"emails":["jhon@gmail.com","jhon@other.com"]},
"relationships": (,..}
}
}
...
型号:
export default Model.extend({
id: attr('string'),
name: attr('string'),
emails: attr()
})
模板:
{{#each model.emails as |email|}}
<input type="text" value={{email}}>
{{/each}}
这就足够了:
export default Model.extend({
id: attr('string'),
name: attr('string'),
emails: attr()
})
因为电子邮件是你上面的一个列表json,所以你的对象设计最好使用一对多关系,如下所示:- 这里有 2 个对象 1]employee 2]emailList
app/models/employee.js
import DS from 'ember-data';
export default Model.extend({
id: DS.attr('string'),
name: DS.attr('string'),
emails: DS.hasMany('email-list'),
})
app/models/email-list.js
import DS from 'ember-data';
export default Model.extend({
email: DS.attr('string'),
employee: DS.belongsTo('employee')
})