0

我正在使用 ember django 适配器,因为我的后端是在 python/django 中设计的。(http://dustinfarris.com/ember-django-adapter/

feed.js(模型)

import DS from 'ember-data';

const {attr} = DS

export default DS.Model.extend({
     gallery_name:attr('string'),
     thumbnail_url:attr('string'),
     is_following:attr('boolean'),
     time:attr('string'),
     description:attr('string'),
     feedPhotos:DS.hasMany('feedPhoto',{embedded:'always'})
});

feedphoto.js(模型)

import DS from 'ember-data';

export default DS.Model.extend({
  feed: DS.belongsTo('feed'),
  url: DS.attr(),
  profilePic: DS.attr(),
  userName: DS.attr(),
  userKarma: DS.attr(),
  caption: DS.attr(),
  numComments: DS.attr(),
  owner: DS.attr(),
  time: DS.attr(),
  photo_url: DS.attr(),
  comments_url: DS.attr(),
  numFives: DS.attr(),
  fivers_url: DS.attr(),
  fivers_pk: DS.attr(),
  fullphoto_url: DS.attr(),
  fullphoto_pk: DS.attr(),
  is_fived: DS.attr('boolean'),
  hiFiveKarma: DS.attr(),
  owner_pk: DS.attr(),
  userFirstName: DS.attr(),
  is_bookmarked: DS.attr('boolean')
});

feed.js(序列化器)

import DRFSerializer from './drf';
import DS from 'ember-data';

export default DRFSerializer.extend(DS.EmbeddedRecordsMixin,{
  primaryKey: 'pk',
  attrs:{
    feedPhotos:{ embedded: 'always' }
  }
});

feedphoto.js(serailizer)

import DRFSerializer from './drf';
import DS from 'ember-data';

export default DRFSerializer.extend({
  primaryKey: 'pk',
});

响应.json

[{
    "pk": 127,
    "url": "http://example.com/api/galleries/127/",
    "gallery_name": "Faces",
    "thumbnail_url": "https://dz.cloudfront.net/galleryThumbs/2656a05c-4ec7-3eea-8c5e-d8019454d443.jpg",
    "time": "1 month ago",
    "description": "Created by user",
    "is_following": true,
    "feedPhotos": [{
        "pk": 574,
        "url": "http://examle.com/api/photos/574/",
        "profilePic": "https://d3.cloudfront.net/userDPs/b6f69e4e-980d-3cc3-8b3e-3eb1a7f21350.jpg",
        "userName": "Rohini",
        "userKarma": 194,
        "caption": "Life @ Myanmar!",
        "numComments": 0,
        "owner": "http://example.cloud.net/api/users/45/",
        "time": "2 months ago",
        "photo_url": "https://example.cloud.net/photos/eeae72d5-d6af-391e-a218-b442c0c7e34e.jpg",
        "comments_url": "http://example.cloud.net/api/photos/574/comments/",
        "numFives": 2,
        "fivers_url": "http://example.cloud.net/api/photogalleries/1303/fivers/",
        "fivers_pk": 1303,
        "fullphoto_url": "http://example.cloud.net/api/photogalleries/1303/photo/",
        "fullphoto_pk": 1303,
        "is_fived": false,
        "hiFiveKarma": 0,
        "owner_pk": 45,
        "userFirstName": "Rohini",
        "is_bookmarked": false
    }, {
        "pk": 446,
        "url": "http://example.cloud.net/api/photos/446/",
        "profilePic": "https://example.cloud.net/userDPs/b359fab0-211d-32b5-8f13-f5edbeb0fbf9.jpg",
        "userName": "Shushma",
        "userKarma": 224,
        "caption": "",
        "numComments": 0,
        "owner": "http://example.cloud.net/api/users/34/",
        "time": "2 months ago",
        "photo_url": "https://example.cloud.net/photos/a415ed45-b6e5-33e0-a17e-6452ddb2f258.jpg",
        "comments_url": "http://example.cloud.net/api/photos/446/comments/",
        "numFives": 3,
        "fivers_url": "http://example.cloud.net/api/photogalleries/1315/fivers/",
        "fivers_pk": 1315,
        "fullphoto_url": "http://example.cloud.net/api/photogalleries/1315/photo/",
        "fullphoto_pk": 1315,
        "is_fived": false,
        "hiFiveKarma": 0,
        "owner_pk": 34,
        "userFirstName": "Shushma",
        "is_bookmarked": false
    }]
}]

该代码能够保存 feed 模型的详细信息,但不能保存 feedphoto 模型。在我的 ember 检查器中,feed 模型显示了正确的数据。但是 feedphoto 模型没有显示任何数据。我不知道为什么我会出错。

4

1 回答 1

0

我认为你有两个问题

  1. 中的大写字母:DS.hasMany('feedPhoto',{embedded:'always'}). 这意味着您的模型文件应命名为“feed-photo.js”或“feed_photo.js”。
  2. 另外,我还没有使用过 ember-django-adapter,但他们通常将feed_photos即将到来的响应解释为feedPhotoember 关系。因此,您可能应该feed_photos从您的服务器返回,而不是feedPhotos将属性重命名为feedphotos
于 2015-12-18T08:04:11.913 回答