Ember BelongsTo within two models 问题

Ember BelongsTo within two models issue

大家好,

型号:

我在课程控制器中为全日历创建结构

    events: function(){
       var lessons = this.get('model');
       var events = [];
       lessons.map(function(lesson){
           events.push({
               'title' : lesson.get('title'),
               'start' : lesson.get('start'),
               'end'   : lesson.get('end'),
               'color' : lesson.get('guild.color'), // return undefined
               'lesson': lesson
           });
       });

       return events;
   }.property('model')

events: function(){
       var lessons = this.get('model');
       var events = [];
       lessons.map(function(lesson){
           events.push({
               'title' : lesson.get('title'),
               'start' : lesson.get('start'),
               'end'   : lesson.get('end'),
               'color' : lesson.get('color'), // return undefined
               'lesson': lesson
           });
       });

       return events;
   }.property('model')

但是在解析lesson.guild.color关系时遇到了麻烦 我尝试在从公会计算颜色的模型中添加新的 属性,如下所示:

title: DS.attr('string'),
guild: DS.belongsTo('guild'),
color: function(){ 
   return this.get('guild.color');
}.property('guild.color')

谢谢,英语。 但这仅适用于 Handlebars,因为它解决了所有承诺。

如果有人知道如何从公会实体中正确获取颜色,那就太好了..

嘿,我试试这个:

events: function(){
    var lessons = this.get('model');
    var events = [];
    var guildPromisses = lessons.getEach('guild');
    return Ember.RSVP.all(guildPromisses).then(function(guilds){
        return lessons.map(function(lesson){
            events.push({
                'title': lesson.get('title'),
                'start': lesson.get('start'),
                'end': lesson.get('end'),
                'color': lesson.get('guild.color')
            });
        });
    }).then(function(result){
        return events;
    });

}.property('model'),

这项工作,但我认为它 return 保证 :)

events: function(){
    var _self = this;
    var lessons = _self.get('model');
    var guildPromisses = lessons.getEach('guild');
    var result =  Ember.RSVP.all(guildPromisses).then(function(guilds){
        var events = [];
        lessons.map(function(lesson){
            events.push({
                'title': lesson.get('title'),
                'start': lesson.get('start'),
                'end': lesson.get('end'),
                'color': lesson.get('guild.color')
            });
        });
        _self.set('events', events);
    });
}.property('model'),

就这些了,感谢 Engma,还有这个话题:How to return a promise composed of nested models in EmberJS with EmberData?