[英]How to display a hasMany association
我是ember的新手,並且創建了這樣的模型:
# app/models/skill.js
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
link: DS.attr('string'),
user: DS.hasMany('user')
});
我有這條路線:
# app/routes/skill.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('skill', params.id);
}
});
此路由器:
# app/router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.resource('users', function() {
this.route('index', { path: '/'} );
this.route('signup');
});
this.resource('skills', function() {
this.route('index', { path: '/' });
});
this.resource('skill', { path: '/skills/:id' });
});
export default Router;
和這個模板:
# app/templates/skill.hbs
{{#link-to 'skills.index'}}Back{{/link-to}}
{{model.name}}
{{model.description}}
{{#each user in model.user}}
{{user.nickname}}
{{/each}}
我只想列出一個昵稱的用戶列表。 這是我的api的響應:
{
"skills": {
"id": 1,
"name": "Ember",
"description": "JS Framework",
"link": "emberjs.com"
},
"users": [
{
"id": 1,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T10:40:34.555Z",
"updated_at": "2015-06-21T10:40:34.555Z"
},
{
"id": 2,
"email": "test",
"nickname": "dougui",
"bio": "blabla",
"created_at": "2015-06-21T10:40:50.493Z",
"updated_at": "2015-06-21T10:40:50.493Z"
},
{
"id": 3,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T20:51:29.546Z",
"updated_at": "2015-06-21T20:51:29.546Z"
},
{
"id": 4,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T20:55:11.163Z",
"updated_at": "2015-06-21T20:55:11.163Z"
},
{
"id": 5,
"email": null,
"nickname": null,
"bio": null,
"created_at": "2015-06-21T20:55:51.231Z",
"updated_at": "2015-06-21T20:55:51.231Z"
},
{
"id": 6,
"email": "email",
"nickname": "nickname",
"bio": null,
"created_at": "2015-06-22T00:01:47.089Z",
"updated_at": "2015-06-22T00:01:47.089Z"
}
]
}
這里有關於技能和用戶的所有信息,但是什么也沒有顯示。
為什么我的列表沒有顯示?
謝謝!
現在,我的api正在發送此{"skill":{"id":2,"name":"Rails","description":"test","user_ids":[2]}}
。
我有這個模型:
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
link: DS.attr('string'),
users: DS.hasMany('users', { async: true })
});
我也嘗試過:
users: DS.hasMany('users', { async: true })
我不知道該怎么辦。 當前版本可能有示例嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.