[英]Ember Model Object created but not showing in template
我正在为用户的收藏夹歌曲制作soundCloud json。
这是json文件
我成功访问了收藏夹和用户属性。
这是我的模型功能
model: function(params) {
var artist, favoriteListProxy, self;
self = this;
artist = params.artist;
this.controllerFor('application').set('artistName', artist);
favoriteListProxy = Ember.ArrayProxy.create({
content: []
});
return new Ember.RSVP.Promise(function(resolve, reject) {
return SC.get("/users/" + 'mannaio' + "/favorites", {
limit: 40
}, function(favorites) {
if (favorites.length) {
favorites.forEach(function(item, index, arr) {
var favorite;
favorite = self.createFavoritelist(item, favoriteListProxy);
return self.createUser(item.user, favorite);
});
favorites = favoriteListProxy.get('content');
return resolve(favorites);
}
});
});
},
createFavoritelist: function(favorite, arr) {
var record;
record = this.store.createRecord('favorite', {});
record.setProperties({
id: favorite.id,
title: favorite.title,
artwork_url: favorite.artwork_url,
genre: favorite.genre,
});
arr.pushObject(record);
return record;
},
createUser: function(user, arr) {
var record;
record = this.store.createRecord('user', {});
record.setProperties({
id: user.id,
username: user.username,
});
arr.pushObject(record);
return record;
},
如您所见,我可以使用属性创建我的收藏夹和用户模型,请参见控制台
然后在我的模板中有一个问题,我可以显示所有喜欢的属性(标题和流派)但不能显示我的user.username
<section class="streaming__favorites">
{{#each favorite in sortedFavorites}}
<article class="streaming__favorites__song">
<span>{{favorite.title}}</span>
<span>{{favorite.genre}}</span>
<span>{{user.username}}</span>
</article>
{{/each}}
</section>
sortProperties: ['created_at:desc'],
sortedFavorites: Ember.computed.sort('model', 'sortProperties')
我在模板中做什么,以不显示user.username模型属性? 我怎么了
首先user
不在该范围内。 您的模型挂钩仅返回favorites
列表。
其次,我不确定此方法的工作方式,我认为它应该崩溃了:
createUser: function(user, arr) {
var record;
record = this.store.createRecord('user', {});
record.setProperties({
id: user.id,
username: user.username,
});
// arr here is the favorite record, not an array, how is this
// not crashing?
arr.pushObject(record);
return record;
},
第三,您的诺言很有可能无法解决,因为其中一个代码路径无法解决,您的ui会一直处于等待状态。
您需要修改从模型挂钩返回的内容,以包括用户和收藏夹,或者包括这些内容。 我不确定您的数据模型是什么样子,所以我只是将其添加到收藏夹中,并根据需要对其进行修复。 我也不知道SC.get
是什么,因此我将其保持在您所显示的位置附近,但看起来可能会过分杀伤。
大概是这样的:
model: function(params) {
var artist, favoriteList, self, ret;
self = this;
artist = params.artist;
ret = [];
this.controllerFor('application').set('artistName', artist);
return new Ember.RSVP.Promise(function(resolve, reject) {
return SC.get("/users/" + 'mannaio' + "/favorites", {
limit: 40
}, function(favorites) {
if (favorites.length) {
favorites.forEach(function(item, index, arr) {
var favorite;
favorite = self.createFavoritelist(item);
ret.push(favorite);
favorite.user = self.createUser(item.user);
});
}
resolve(ret);
});
});
},
createFavoritelist: function(favorite) {
return this.store.createRecord('favorite', {
id: favorite.id,
title: favorite.title,
artwork_url: favorite.artwork_url,
genre: favorite.genre,
});
},
createUser: function(user) {
return this.store.createRecord('user', {
id: user.id,
username: user.username,
});
},
那你的模板就是这个
<section class="streaming__favorites">
{{#each favorite in sortedFavorites}}
<article class="streaming__favorites__song">
<span>{{favorite.title}}</span>
<span>{{favorite.genre}}</span>
<span>{{favorite.user.username}}</span>
</article>
{{/each}}
</section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.