HasMany创建记录的关系 - 为什么未定义?

时间:2013-12-12 00:48:14

标签: javascript has-many

我有一个与“创建记录的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

1 个答案:

答案 0 :(得分:0)

Phew,花了一段时间才弄明白。我有一个[最小的,没有造型或钟声和口哨]工作示例:https://gist.github.com/polerc/8137422

<强>要点:

基本上,TopicsControllerQuestionsController分层太多。将TopicsController合并到QuizController并将QuestionsController合并到TopicController