Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 余烬罐';t access在父控制器中有许多模型_Javascript_Ember.js_Controller - Fatal编程技术网

Javascript 余烬罐';t access在父控制器中有许多模型

Javascript 余烬罐';t access在父控制器中有许多模型,javascript,ember.js,controller,Javascript,Ember.js,Controller,我想访问父控制器中的子模型,以检查是否所有子模型都已完成。当我第一次在课程(父)页面时,变量topicsTotal和topicsCompleted返回0,但当我转到特定主题(子)页面,然后返回此课程页面时,变量topicsTotal和topicsCompleted用正确的数字填充。它看起来像函数allCompleted();在加载所有模型之前调用控制器内部。我怎样才能解决这个问题 这是课程控制器: BecomeTjunaFish.LessonController = Ember.ObjectCo

我想访问父控制器中的子模型,以检查是否所有子模型都已完成。当我第一次在课程(父)页面时,变量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]
    }
这是lessonview:

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

});

你试过用“then”吗?类似于:this.get('topics')。然后(function(topics){//var topicscotal=this.get('topics.length')之后的所有代码都在这里,并且您还有'topics'变量,所以您可以执行topics.filterBy('isCompleted',true.)。get('length'))谢谢fanta!这是函数现在的工作方式:allCompleted:function(){this.get('topics')。then(function(topics){var topicscotal=topics.get('length'),topicsCompleted=topics.filterBy('isCompleted',true')。get('length');if(topicscotal==topicsCompleted){$('lessonBadge')).addClass('completed');}});很酷,很高兴您可以继续您的项目:)