在我的应用程序中,我有Course
模型TeamMember
,以便每个团队成员可以负责几门课程:
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}}
在此先感谢您的帮助。