hasMany没有更新ember商店

时间:2015-12-15 09:45:48

标签: json ember.js ember-data django-adaptors

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

feed.js(model)

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',
});

response.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
    }]
}]

代码能够保存饲料模型细节,但不能保存饲料照片模型。在我的余烬检查器中,饲料模型显示正确的数据。但是饲料照片模型没有显示任何数据。我不知道为什么我出错了。

1 个答案:

答案 0 :(得分:0)

我认为你有两个问题

  1. 大写字母:DS.hasMany('feedPhoto',{embedded:'always'})。 这意味着,您的模型文件应该命名为' feed-photo.js'或者' feed_photo.js'。
  2. 此外,我还没有使用过ember-django-adapter,但他们通常会将feed_photos作为feedPhoto余烬关系作为回应来解释。因此,您应该从服务器返回feed_photos而不是feedPhotos,或者将该属性重命名为feedphotos