createRecord with ember-data + ember-data-django-rest-adapter

时间:2014-01-15 19:17:51

标签: django ember.js ember-data

我正在使用带有Django后端的ember / ember-data / ember-data-django-rest-adapter创建一个ember应用程序。

当有belongsTo和hasMany关系时,我遇到创建记录的问题。

我目前有这段代码:

App.Article = DS.Model.extend({
  title: attr(),
  description: attr(),
  authors: hasMany('author'),
  category: belongsTo('page'),
  slug: attr(),
  content: attr(),
  articleContent: Ember.computed.alias("content"),
  published: attr(),
  publish_from: attr(),
  isScheduled: function() {
    return moment().isBefore(moment(this.get('publish_from')));
  }.property('publish_from'),
  articlePublishDate: function() {
    return moment(this.get('publish_from')).format('MMMM Do YYYY');
  }.property('publish_from'),
  articlePublishTime: function() {
    return moment(this.get('publish_from')).format('h:mm a');
  }.property('publish_from'),
  //content_type: belongsTo('content_type', { async: true }),
  content_type: attr()
});

App.Page = DS.Model.extend({
  title: attr(),
  description: attr(),
  pageContent: attr(null, {
    key: 'content'
  }),
  templateFile: attr(null, {
    key: 'template'
  }),
  slug: attr(),
  tree_path: attr(),
  tree_parent: belongsTo('page'),
  site: attr()
});

App.Author = DS.Model.extend({
  name: attr(),
  slug: attr(),
  description: attr(),
  text: attr(),
  email: attr(),
  photo: attr(),
  user: belongsTo('user'),
});

// create article
App.ArticleCreateController = Ember.ObjectController.extend({

  editMode: false,

  allAuthors: function() {
    return this.store.find('author');
  }.property(),

  allPages: function() {
    return this.store.find('page');
  }.property(),

  actions: {

    save: function(session) {
      var self = this;
      var article = this.get('model');

      var newArticle = this.store.createRecord('article', {
        content_type: "19",
        content: article.get('articleContent'),
        description: article.get('description'),
        publish_from: article.get('publish_from'),
        published: article.get('published'),
        slug: article.get('slug'),
        title: article.get('title')
      });

      this.store.find('page', 3).then(function(page) {
        newArticle.set('category', page);
      });

      newArticle.save();

    }

  }
});

我真正想做的就是将这样的POST数据发送到apiRoot / articles /(以及其他属性,但这些属性应该按照它们的方式运行)

  

作者:[1,3,5],// hasMany

     

类别:3 // belongsTo

但是当我发出POST请求时,由于某种原因,category返回null。我想要从中提取的只是id本身。另外,我不知道如何提取作者数组。我尝试发布数据,它告诉我一些关于它需要'App.Author'。

1 个答案:

答案 0 :(得分:2)

首先,在当前你需要一个ember-data的分支,因为async create目前已被破坏(因为它是一个承诺而内部序列化程序不会等待它解析)。

下拉此分支,执行npm install + grunt测试以构建适配器。你还需要在那个branch'es测试lib目录中使用ember-data的分叉构建(直到ember-data引入修复程序)

https://github.com/toranb/ember-data-django-rest-adapter/tree/asyncBelongsToHasManyWIP

然后在你的控制器内你可以做这样的事情来“创建”客户和约会(注意-async belongsTo / hasMany关系)

App.Customer = DS.Model.extend({
  name: DS.attr('string'),
  appointments: DS.hasMany('appointment', { async: true})
});

App.Appointment = DS.Model.extend({
  details: DS.attr('string'),
  customer: DS.belongsTo('customer', { async: true})
});

var customer = {
  name: 'foobar'
}
this.store.createRecord('customer', customer).save().then(function(persisted) {
  var appointment = {
    details: 'test',
    customer: persisted
  }
  return self.store.createRecord('appointment', appointment).save().then(function(apt) {
    persisted.get('data').appointments.pushObject(apt);
    router.transitionTo('index');
  });
});