Ember无法访问父控制器中的hasmany模型

时间:2014-06-11 14:29:16

标签: javascript ember.js controller

我想访问父控制器中的子模型,以检查是否所有子模型都已完成。当我第一次进入课程(父级)页面时,变量topicsTotal和topicsCompleted变量返回0,但是当我转到特定主题(子)页面然后返回到本课程页面时,变量topicsTotal和topicsCompleted变量将填充正确的数字。它看起来像函数allCompleted();在加载所有模型之前调用控制器内部。我怎样才能解决这个问题?

这是课程控制器:

BecomeTjunaFish.LessonController = Ember.ObjectController.extend({

  allCompleted: function() {
    var topicsTotal = this.get('topics.length'),
      topicsCompleted = this.get('topics').filterBy('isCompleted', true).get('length');
      console.log(topicsTotal);
      console.log(topicsCompleted);
      if (topicsTotal == topicsCompleted){
        $('.lessonBadge').addClass('completed');
      }
  }

});

这是课程模型:

  BecomeTjunaFish.Lesson = DS.Model.extend({
    title: DS.attr('string'),
    progress: DS.attr('number'),
    img: DS.attr('string'),
    goal: DS.attr('string'),
    targetGroup: DS.attr('string'),
    prerequisites: DS.attr('string'),
    lesson_level: DS.attr('string'),
    lesson_time_indication: DS.attr('string'),
    lesson_url: DS.attr('string'),
    course: DS.belongsTo('Course', {async: true}),
    topics: DS.hasMany('Topic', {async: true}),
    lesson_authors: DS.hasMany('Authors', {async: true}),
    assignments: DS.hasMany('Assignment', {async: true})
  });

  BecomeTjunaFish.Lesson.FIXTURES = [
    {
      id: 1,
      title: 'HTML Basis',
      progress: 50,
      img: 'images/html_badge.png',
      goal: 'HTML basis concepten beheersen',
      targetGroup: 'frontend developers in wording',
      prerequisites: 'geen',
      lesson_level: 'Beginner',
      lesson_time_indication: '45 minuten',
      course: 1,
      topics: [1,2,3],
      lesson_authors: [1, 2],
      assignments: [1]
    }

这是课程观点:

BecomeTjunaFish.LessonView = Ember.View.extend({
    didInsertElement: function (){
        this._super();
        this.get('controller').allCompleted();
    }

});

0 个答案:

没有答案