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의 관계를 포함 확인할 수 있습니다 크롬 타다 확장을 사용
는, 그러나 여전히 "알 수없는 아티스트"로 표시됩니다.아티스트 모델 속성을 표시하려면 템플릿을 조정해야하나요 (선택 사항 : 이름이 여러 개인 경우 쉼표로 구분).
흠 내 나쁜, 엠버 확장이 belongsTo를 속성이 기본적 대신 널 (null)에 의해 모든 hasMany의 속성에 DS.manyArray를 할당 보인다, 먼저 지금 디버깅해야합니다. – xorinzor