我有一个问题涉及到“创建记录的HasMany关系”,但我已经查看并尝试了堆栈溢出中已有的答案,但它们不起作用。问题在于模型未定义。我已经结合了QuestionsController和TopicController。HasMany关系创建记录 - 为什么未定义?
这是主题模型:
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