我目前正在使用带有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 /(以及其他属性,但这些属性应有的运作方式)

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

最佳答案

首先,由于异步创建当前已中断(由于这是一个 promise ,并且内部序列化程序将不等待其解决),因此当前您需要使用ember-data分支。

拉下该分支,执行npm install + grunt测试以构建适配器。另外,您还需要在该分支机构的测试lib目录中使用ember-data的分叉版本(直到为此目的提供了ember-data的修复)

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

然后,在您的 Controller 内部,您可以执行以下操作来“创建”客户和约会(注意-async属于/有很多关系)

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

关于django - 带有ember-data + ember-data-django-rest-adapter的createRecord,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21146280/

10-14 22:45