无法使用EmberJS和Ember数据从RoR后端获取JSON响应

时间:2013-07-05 08:59:31

标签: ruby-on-rails json ember-data serializer ember-1

我知道这是一个有点棘手的问题。问题是我通过JSON响应在我的Ruby on Rails后端和Ember / Ember-Data之间成功连接了两个具有复杂关系的模型(在它们之间使用连接表等)。

知道,问题是我无法连接第三个模型。它的ID嵌入另一个工作正常的模型。事情是,我确实得到了一个有效的JSON响应数据,但我的余烬数据模型在我的余烬控制器上由于某种原因没有得到它们。

这是我想要获得的模型:

Profile.js

App.Adapter.map
(
  'App.Profile',
  {
    projects: {embedded: 'load'},
    people: {embedded: 'load'}
  }
);

App.Profile = DS.Model.extend
(
   {
      name: DS.attr('string'),
      tools: DS.attr('string'),
      number: DS.attr('number'),
      projects: DS.hasMany('App.Project', {embedded: 'load'}),
      people: DS.hasMany('App.Person', {embedded: 'load'})
   }
);

模型 Person.js ,其JSON响应有效返回:

App.Adapter.map
(
  'App.Person',
  {
    profiles: {embedded: 'load'},
    projectsCreated: {embedded: 'load'},
    projects: {embedded: 'load'},
    friends: {embedded: 'load'}
  }
);

App.Person = DS.Model.extend
(
   {
      name: DS.attr('string'),
      place: DS.attr('string'),
      email: DS.attr('string'),
      password: DS.attr('string'),
      academic: DS.attr('string'),
      professional: DS.attr('string'),
      profiles: DS.hasMany('App.Profile', {embedded: 'load'}),
      knowledge: DS.attr('string'),
      projectsCreated: DS.hasMany('App.Project', {embedded: 'load'}),
      projects: DS.hasMany('App.Project', {embedded: 'load'}),
      friends: DS.hasMany('App.Person', {embedded: 'load'}),
      iconUrl: DS.attr('string')
   }
);

每个序列化工具 profile_serializer.rb person_serializer.rb

class ProfileSerializer < ActiveModel::Serializer
  attributes :id,
             :name,
             :tools,
             :number

  has_many :projects, ember: :id
  has_many :people, ember: :id           
end

class PersonSerializer < ActiveModel::Serializer
  attributes :id,
             :name,
             :place,
             :email,
             :password,
             :academic,
             :professional,
             :knowledge,
             :icon_url

  has_many :friends, embed: :id
  has_many :projects_created, embed: :id
  has_many :projects, embed: :id
  has_many :profiles, embed: :id
end       

最后, JSON回复

GET http://localhost:3000/people 

{
    "people": [
        {
            "id": 1,
            "name": "Daniel",
            "place": "Ireland",
            "email": "daniel@example.com",
            "password": "123456",
            "academic": "Computing Engineer",
            "professional": "Fundecor",
            "knowledge": "html, css, javascript, jquery, ember",
            "icon_url": "http://imageshack.us/a/img196/6619/xckl.jpg",
            "friend_ids": [
                2,
                3
            ],
            "projects_created_ids": [
                1
            ],
            "project_ids": [],
            "profile_ids": [
                1,
                3,
                5
            ]
        },
        {
            "id": 2,
            ...
        },
        {
            "id": 3,
            ...
        }
    ]
}

下一步是查询这个profile_ids,其正确答案是:

GET http://localhost:3000/profiles?ids%5B%5D=1&ids%5B%5D=3&ids%5B%5D=5

{
    "profiles": [
        {
            "id": 1,
            "name": "Frontend",
            "tools": "html, css, javascript, jquery",
            "number": 0,
            "projects": [],
            "people": [
                {
                    "id": 1,
                    "name": "Daniel",
                    "place": "Ireland",
                    "email": "daniel@example.com",
                    "password": "123456",
                    "academic": "Computing Engineer",
                    "professional": "Fundecor",
                    "knowledge": "html, css, javascript, jquery, ember",
                    "icon_url": "http://imageshack.us/a/img196/6619/xckl.jpg",
                    "friend_ids": [
                        2,
                        3
                    ],
                    "projects_created_ids": [
                        1
                    ],
                    "project_ids": [],
                    "profile_ids": [
                        1,
                        3,
                        5
                    ]
                }
            ]
        },
        {
            "id": 2,
            "name": "Mobile Developer",
            ...
        },
        ...
    ]
}

返回所有配置文件条目。但问题是,当我尝试通过我的控制器或我的路线获取此信息时,浏览器控制台(firebug或类似信息)上的结果为空。人的每个领域都是正确填充的,包括profile_ids,但不包括profile模型本身。

也许,如果您想拥有整个代码,可以在https://github.com/neil89/project-on上查看。非常感谢您的回答。

0 个答案:

没有答案