2013-10-08 176 views
1

在我的應用我有CourseTeamMember模式,讓每個團隊成員都可以爲幾門課程負責:迭代通過DS.hasMany

FrontApp.TeamMember = DS.Model.extend({ 
    name: DS.attr('string'), 
    email: DS.attr('string'), 
    image: DS.attr('string'), 
    courses: DS.hasMany('course', {async: true}) 
}); 

FrontApp.Course = DS.Model.extend({ 
    title:  DS.attr('string'), 
    ... 
    bunch of other model-specific fields (no relations) 
    ... 
    ects:  DS.attr('number') 
}); 

現在我要顯示的個人資料頁上的以下:

  • is responsible for COURSE_TITLE_1, COURSE_TITLE_2如果他是負責< = 2個療程
  • is responsible for COURSE_TITLE_1, COURSE_TITLE_2, ...如果他是負責> 2個療程

目前,我嘗試這樣做,從itemController:

FrontApp.TeamMemberController = Ember.ObjectController.extend({ 
    responsibleForCourses: function(){ 
    var res = ""; 

    this.get('courses').then(function(loaded_courses){ 
     if (loaded_courses.get('length') <= 2) { 
      loaded_courses.forEach(function(course){ 
       res += course.get('title') + " | "; 
      }) 
     } else { 
      // access first two of loaded_courses here and add "..." 
     } 
     console.log(res); // this thing works 
    }) 

    return res; // but here it does not work 
    }.property('courses') 
}); 

由於model.courses是一個承諾,我不得不使用.then(),但是這引起了以下問題:我怎麼回我res和將其顯示在我的模板中?

順便說一句,我的模板看起來是這樣的:

responsible for courses: {{responsibleForCourses}} 

在此先感謝您的幫助。

+0

附:我開始認爲我試圖以錯誤的方式做到這一點:也許我應該使用視圖或其他用於此目的... – VoloD

回答

0

您應該返回resthen回調中:

FrontApp.TeamMemberController = Ember.ObjectController.extend({ 
    responsibleForCourses: function(){ 
    var res = ""; 
    return this.get('courses').then(function(loaded_courses){ 
     if (loaded_courses.get('length') <= 2) { 
     loaded_courses.forEach(function(course){ 
      res += course.get('title') + " | "; 
     }) 
     } else { 
     // access first two of loaded_courses here and add "..." 
     } 
     return res; 
    }) 
    }.property('courses') 
}); 

希望它能幫助。

+0

嘗試過,但它不起作用=( 如果我確實返回insnde .then )它不返回任何東西 – VoloD

+0

@VoloD對不起,只是添加到我的答案'返回'承諾本身...它的工作呢? – intuitivepixel

+0

nope =(它返回承諾(從控制檯): 承諾{_promiseCallbacks:對象,構造函數:函數,isRejected:undefined,isFulfilled:undefined,rejectedReason:undefined ...} – VoloD

2

字符串在JavaScript中是不可變的,所以一旦你返回空字符串""你不能在以後改變它。要解決該問題,可以在承諾解決時設置屬性。

FrontApp.TeamMemberController = Ember.ObjectController.extend({ 
    responsibleForCourses: function(){ 
    var that = this; 
    this.get('courses').then(function(loaded_courses){ 
     var final_result = ""; 
     if (loaded_courses.get('length') <= 2) { 
     loaded_courses.forEach(function(course){ 
      final_result += course.get('title') + " | "; 
     });   
     } else { 
     // access first two of loaded_courses here and add "..." 
     } 
     that.set('responsibleForCourses', final_result); 
    }); 
    return ""; // return "" as a temporary value until the promise is resolved 
    }.property('courses') 
}); 
0

感謝您的回答,我解決了一個不同的方式問題:

FrontApp.MyTeamController = Ember.ObjectController.extend({ 
    firstCourses: Ember.computed(function(){ 
    return this.get('courses').slice(0,2); 
).property('[email protected]'), 

    otherCourses: Ember.computed(function(){ 
    return this.get('courses').slice(2); 
    }).property('[email protected]') 
}) 

據我瞭解,這個問題是.property('courses')而不是.property('[email protected]')