我有一個問題涉及到「創建記錄的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