简体   繁体   English

如何显示hasMany关联

[英]How to display a hasMany association

I'm new to ember and I created a model like this : 我是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')
});

I have this route : 我有这条路线:

# app/routes/skill.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('skill', params.id);
  }
});

This router : 此路由器:

# 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;

And this template : 和这个模板:

# app/templates/skill.hbs
{{#link-to 'skills.index'}}Back{{/link-to}}
{{model.name}}
{{model.description}}

{{#each user in model.user}}
  {{user.nickname}}
{{/each}}

I simply want to have a list of users with their nicknames. 我只想列出一个昵称的用户列表。 This is the response from my api : 这是我的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"
    }
  ]
}

There is all information about skill and users but nothing is shown. 这里有关于技能和用户的所有信息,但是什么也没有显示。

Why my list is not rendered? 为什么我的列表没有显示?

Thanks! 谢谢!

Edit 1 编辑1

Now, my api is sending this {"skill":{"id":2,"name":"Rails","description":"test","user_ids":[2]}} . 现在,我的api正在发送此{"skill":{"id":2,"name":"Rails","description":"test","user_ids":[2]}}

I have this model : 我有这个模型:

export default DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  link: DS.attr('string'),
  users: DS.hasMany('users', { async: true })
});

I tried also with : 我也尝试过:

users: DS.hasMany('users', { async: true })

I don't know what to do. 我不知道该怎么办。 Is it possible to have an example with the current version? 当前版本可能有示例吗?

Ember data expects that you will give it a object called skill not skills ( skills will be ok for requesting multiple skills, not one instance). 灰烬数据希望您会给它一个对象,称为skill而非skillsskills可以请求多个技能,而不是一个实例)。

Next thing: your model expects users array in skill object. 下一件事:您的模型期望usersskill对象中进行排列。 Right now you are sending it separately. 现在,您将单独发送它。

And last one... You have put full users object instead of just ids of these objects. 最后一个...您已经放置了完整的users对象,而不仅仅是这些对象的ID。 In that case, you have to add flag {embedded: 'always'} to users property (read more here ) 在这种情况下,您必须向users属性添加标记{embedded: 'always'}在此处了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM