具有ember-data + ember-data-django-rest-adapter的createRecord [英] createRecord with ember-data + ember-data-django-rest-adapter

查看:110
本文介绍了具有ember-data + ember-data-django-rest-adapter的createRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ember / ember-data / ember-data-django-rest-adapter与Django后端创建一个ember应用程序。

I'm currently working on creating an ember application using ember/ember-data/ember-data-django-rest-adapter with Django backend.

当有属于并有许多关系发生时,有问题创建记录。

I'm having issue creating record when there's belongsTo and hasMany relationship going on.

我目前有这样的代码:

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 /(以及其他属性,但那些正在按照他们的方式工作)

All I really want to do is POST data like this to apiRoot/articles/ (along with other attributes, but those are working the way they should)


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

authors: [1,3,5], // hasMany

类别:3 // belongsTo

category: 3 // belongsTo

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

But when I make a POST request, category returns as null for some reason. All I want to extract from it is just the id itself. Also, I have no clue how to extract the array of authors. I tried posting the data, and it tells me something about it needing to be 'App.Author'.

推荐答案

首先,当前时间你需要一个垃圾数据的叉子,因为异步创建当前被打破(因为它是一个承诺,内部的串行器不会等待它解决)。

First, at the current time you need a fork of ember-data because async create is currently broken (as it's a promise and the internal serializer won't wait for it to resolve).

拉下这个分支,做一个npm安装+ grunt测试来构建适配器。此外,您需要使用该分支的测试lib目录中的分支构建的ember数据(直到ember数据拉入此修复程序)

Pull down this branch, do a npm install + grunt test to build the adapter. Also you need to use the forked build of ember-data in that branch'es test lib directory (until ember-data pulls in the fix for this)

https://github.com/toranb/ember-data-django-rest-适配器/树/ asyncBelongsToHasManyWIP

然后在控制器内部,可以这样做,以创建客户和约会(注意-async belongsTo / hasMany关系)

Then inside your controller you can do something like this to "create" the customer and appointment (notice -async belongsTo/hasMany relationship)

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

这篇关于具有ember-data + ember-data-django-rest-adapter的createRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆