В настоящее время я работаю над созданием приложения ember с использованием ember/ember-data/ember-data-django-rest-adapter с бэкэндом Django.createRecord с данными ember-data + ember-data-django-rest-adapter
У меня возникла проблема с созданием записи, когда есть что-то связанное с отношениями 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/статьи/(наряду с другими атрибутами, но те, которые работают так, как они должны)
авторы: [1,3,5], // hasMany
категория: 3 // belongsTo
Но когда я делаю запрос POST, категория по какой-то причине возвращает null. Все, что я хочу извлечь из него, это просто сам идентификатор. Кроме того, я не знаю, как извлечь массив авторов. Я попытался опубликовать данные, и он говорит мне что-то о том, что нужно быть «App.Author».
Работал отлично. Ты обалденный. Спасибо! –
Спасибо! теперь я просто надеюсь, что он втянут в ядро ember-data: D –