2016-01-13 64 views
2

我正在尝试计算有多少人参与了扫描。 在这一刻我需要2个字:通过模型生成的计数(通过数据透视表)未显示

  • 总参与数
  • 共完成数

我取得了灰烬透视表,它包含有关参与者的具体数据,就像他完成了扫描或不。

我的模型:

import DS from 'ember-data'; 

var inflector = Ember.Inflector.inflector; 
inflector.irregular('scan', 'scan'); 

var scanModel = DS.Model.extend({ 
    title:    DS.attr('string'), 
    mailInviteSubject: DS.attr('string'), 
    mailInviteMessage: DS.attr('string'), 
    mailResponseAddress: DS.attr('string'), 
    dateDeadline:  DS.attr('date'), 
    scanGroup:   DS.belongsTo('scanGroup',  {async: true}), 
    questionGroups:  DS.hasMany('question-group', {async: true}), 


    /** 
    * Participants 
    */ 
    scanParticipants:   DS.hasMany('scanParticipants', {async: true}), 
    participants:    Ember.computed('[email protected]', function() { 
     return this.get('scanParticipants').then(function(scanParticipants) { 
      return scanParticipants.mapBy('participant'); 
     }) 
    }), 
    participantsCount:   Ember.computed('scanParticipants', function() { 
     return this.get('scanParticipants').then(function (participants) { 
      return participants.get('length'); 
     }); 
    }), 
    participantsCountFinished: Ember.computed('scanParticipants', function() { 
     return this.get('scanParticipants').then(function (participants) { 
      return participants.filterBy('finished', true).get('length'); 
     }); 
    }), 

    isClosed:  Ember.computed('dateDeadline', function() { 
     let scanDate = moment(this.get('dateDeadline')); 
     let date  = moment(); 

     if(scanDate < date) 
      return true; 
     else 
      return false; 
    }), 
}); 

export default scanModel; 

我有以下模板:

<td>{{input type="checkbox" checked=isChecked action="scanChecked"}}</td> 
<td> 
    <h2>{{scan.title}}</h2> 
    Verloopt op {{format-date scan.dateDeadline format="DD MMMM"}} <small class="_muted">({{#if scan.isClosed}}Afgerond{{else}}Over{{/if}} {{format-date scan.dateDeadline type='remaining'}} geleden)</small> 
</td> 
<td> 
    <h2>{{scan.participantsCount}}</h2> 
    <small class="_muted">uitgenodigd</small> 
</td> 
<td> 
    <h2>{{scan.participantsCountFinished}}</h2> 
    <small class="_muted">voltooid</small> 
</td> 
<td class="_align-right"> 
    {{#link-to 'scangroup.scan' scan.id class="btn btn-inverted btn-small"}}Bekijk{{/link-to}} 
</td> 

现在的问题是,是,{{scan.participantsCount}}{{scan.participantsCountFinished}}显示在我的模板,而不是数[Object Object]

但是,如果我记录承诺的计数,我会得到应该显示在模板中的好计数。

它是如何显示的[对象对象]而不是计数,我怎样才能显示计数?

在此先感谢!

请问候,

帕斯卡尔

回答

0

你所观察的属性本身,而不是数组。这意味着它们只会在scanParticipants更改值时重新计算,例如this.set('scanParticipants', someValue),而不是更新时的值。要解决这个问题,你可以做Ember.computed('scanParticipants.[]', function() {

您正在看到[Object Object],因为这是从计算属性返回的承诺。当您从计算属性返回承诺时,您需要将其包装在PromiseObject或PromiseArray中,但Ember Data已经为您处理关系。
有几个办法可以解决这个问题:

  • 您可以在模板中使用{{scan.scanParticipants.length}}
  • 你可以声明participantsCount: Ember.computed.alias('scanParticipants.length')
  • 你可以做participantsCount: Ember.computed('scanParticipants.[]', function() { return this.get('scanParticipants.length'); })

请注意,您也有一个Ember.computed.filterBy方法。