我有一个与“创建记录的HasMany关系”相关的问题,但我已经查看并尝试了已经存在堆栈溢出的答案,但它们不起作用。问题是该模型未定义。我已将QuestionsController和TopicController结合起来。
这是主题的模型:
App.Topic = DS.Model.extend({
title: DS.attr('string'),
questions: DS.hasMany('Question', {async: true}),
});
App.Topic.FIXTURES = [
{
id: 1,
title: 'Early America',
questions: [1,2]
},
{
id: 2,
title: 'American Revolution',
},
{
id: 3,
title: 'Modern America',
}
];
这是TopicsController:
App.TopicsController = Ember.ArrayController.extend({
actions: {
createTopic: function () {
var Topic = this.store.createRecord('Topic', {
title: 'Untitled Topic'
});
/* Topic.get(questions.find(1)... */
Topic.save();
this.set('newTitle', '');
},
}
});
这是TopicController:
App.TopicController = Ember.ObjectController.extend({
isEditing: false,
actions: {
editTopic: function () {
this.set('isEditing', true);
},
acceptChanges: function () {
this.set('isEditing', false);
},
removeTopic: function () {
var topic = this.get('model');
topic.deleteRecord();
topic.save();
},
createQuestion: function () {
var question = this.get('store').createRecord('Question', {
title: 'Untitled Question',
topic: this.get('model'),
});
question.save();
}
}
});
这是问题的模型:
App.Question = DS.Model.extend({
title: DS.attr('string'),
topic: DS.belongsTo('Topic', {async: true}),
});
App.Question.FIXTURES = [
{
id: 1,
title: 'What continent did Colombus find?',
topic: 1,
},
{
id: 2,
title: 'Other question',
},
];
这是QuestionController:
App.QuestionController = Ember.ObjectController.extend({
isEditing: false,
actions: {
editQuestion: function () {
this.set('isEditing', true);
},
acceptChanges: function () {
this.set('isEditing', false);
},
removeQuestion: function () {
console.log(this);
console.log("hello");
var question = this.get('model');
question.deleteRecord();
question.save();
}
}
});
这是存储所有文件的地方:https://github.com/Glorious-Game-Design-ASL/MapQuizGame/tree/master/quiz_creator
答案 0 :(得分:0)
<强>要点:强>
基本上,TopicsController
和QuestionsController
分层太多。将TopicsController
合并到QuizController
并将QuestionsController
合并到TopicController
。