2013-10-20 1 views
1

에서 hasMany의 모델 속성을 표시합니다 : 어떻게 이러한 모델이 템플릿

var attr = DS.attr, 
    belongsTo = DS.belongsTo, 
    hasMany = DS.hasMany; 

Shoutzor.Album = DS.Model.extend({ 
    artist: belongsTo('artist'), 
    title: attr('string'), 
    cover: attr('string') 
}); 

Shoutzor.Artist = DS.Model.extend({ 
    name: attr('string'), 
    profileimage: attr('string') 
}); 

Shoutzor.User = DS.Model.extend({ 
    name:   attr('string'), 
    firstname:  attr('string'), 
    email:   attr('string'), 
    joined:   attr('date'), 
    last_active: attr('date') 
}); 

Shoutzor.Track = DS.Model.extend({ 
    title: attr('string'), 
    length: attr('number'), 
    artist: hasMany('artist'), 
    album: hasMany('album'), 

    /* Convert the length in seconds to a string like '01:55' */ 
    convertedLength: function() { 
     var sec_num = parseInt(this.get('length'), 10); // don't forget the second parm 
     var hours = Math.floor(sec_num/3600); 
     var minutes = Math.floor((sec_num - (hours * 3600))/60); 
     var seconds = sec_num - (hours * 3600) - (minutes * 60); 

     if (hours < 10 && hours > 0) {hours = "0"+hours;} 
     if (minutes < 10 && minutes > 0) {minutes = "0"+minutes;} 
     if (seconds < 10) {seconds = "0"+seconds;} 
     var time = ((hours != 0) ? hours + ':' : '') + ((minutes != 0) ? minutes +':' : '') + seconds; 

     return time; 
    }.property('length') 
}); 

Shoutzor.History = DS.Model.extend({ 
    track: belongsTo('track'), 
    user: belongsTo('user'), 
    time_played: attr('date'), 

    print_time: function() { 
     var d = new Date(this.get('time_played')); 

     var hours = (d.getHours() < 10 ? "0" : '') + d.getHours(), 
      minutes = (d.getMinutes() < 10 ? "0" : '') + d.getMinutes(), 
      seconds = (d.getSeconds() < 10 ? "0" : '') + d.getSeconds(); 

     return hours + ":" + minutes + ":" + seconds; 
    }.property('time_played') 
}); 

그리고 내 템플릿에이 코드를 사용

(나는 this.store.all하는 모델을 연결하는 내 경로에 ('역사')) :
<table id="songhistory" class="table table-condensed"> 
    <thead> 
     <th width="30%">Title</th> 
     <th width="20%">Artist</th> 
     <th width="20%">Album</th> 
     <th width="15%">Requested by</th> 
     <th width="15%">Time played</th> 
    </thead> 
    <tbody> 
     {{#each model}} 
      <tr> 
       <td>{{track.title}}</td> 
       <td>{{#if track.artist}}{{#each track.artist}}{{name}}{{/each}}{{else}}Unkown Artist{{/if}}</td> 
       <td>{{#if track.album}}{{#each track.abum}}{{title}}{{/each}}{{else}}Unknown Album{{/if}}</td> 
       <td>{{#if user}}{{user.name}}{{else}}AutoDJ{{/if}}</td> 
       <td>{{print_time}}</td> 
      </tr> 
     {{else}} 
      <tr> 
       <td colspan="5">No track history available</td> 
      </tr> 
     {{/each}} 
    </tbody> 
</table> 

지금은 내 트랙 모델은 아티스트 모델에 hasMany의 관계를 포함 확인할 수 있습니다 크롬 타다 확장을 사용

는, 그러나 여전히 "알 수없는 아티스트"로 표시됩니다.

아티스트 모델 속성을 표시하려면 템플릿을 조정해야하나요 (선택 사항 : 이름이 여러 개인 경우 쉼표로 구분).

답변

0

모든 것이 올바르게 보입니다. 각 헬퍼를 지정하고 어레이의 길이를 시험해보고 항목이 실제로 거기에 존재하는지 확인해보십시오.

또한 뭔가가 있어야한다고 생각되면 로그 도우미를 사용하여 콘솔에 로그인 해보십시오. 또한 쉼표

{{#each history in model}} 
    <tr> 
     <td>{{history.track.title}}</td> 
     Artist Count: {{history.track.artist.length}} 
     {{log history.track.artist}} 
     <td>{{#each artist in history.track.artist}}{{artist.name}}{{else}}Unkown Artist{{/each}}</td> 
     <td>{{#each album in history.track.album}}{{album.title}}{{/each}}{{else}}Unknown Album{{/each}}</td> 
     <td>{{#if history.user}}{{history.user.name}}{{else}}AutoDJ{{/if}}</td> 
     <td>{{history.print_time}}</td> 
    </tr> 
{{else}} 
    <tr> 
     <td colspan="5">No track history available</td> 
    </tr> 
{{/each}} 

는 그것이 ItemController의를 사용할 수 있고, 새로운 컨트롤러로 렌더링해야합니다 모델에 목록을 구분합니다. 이 jsbin를 체크 아웃 :

http://emberjs.jsbin.com/eXeTAba/2/edit

+0

흠 내 나쁜, 엠버 확장이 belongsTo를 속성이 기본적 대신 널 (null)에 의해 모든 hasMany의 속성에 DS.manyArray를 할당 보인다, 먼저 지금 디버깅해야합니다. – xorinzor

관련 문제