我正在尝试创建一个帮助文件,该文件将返回任何艺术家相关艺术家的热门曲目。我想做的只是使用 1 个艺术家 URI 来显示他们相关艺术家的姓名、人气和热门曲目。而且我想将顶级轨道功能分离到一个单独的文件中,我可以随时调用。
但是,我似乎无法弄清楚如何正确返回相关艺术家的热门曲目。
在我的“get-toptrack.js”文件中:
require([
'$api/models',
'$api/toplists#Toplist'
], function(models, Toplist) {
'use strict';
var doGetTopTrack = function(uri, num) {
var artistURI = uri;
var artist = models.Artist.fromURI(artistURI);
var artistTopList = Toplist.forArtist(artist);
artistTopList.tracks.snapshot().done(function(snapshot){
snapshot.loadAll('name').done(function(tracks) {
var i, num_toptracks;
num_toptracks = num;
for(i = 0; i < num_toptracks; i++){
console.log("top track: " + tracks[i].name);
// WHERE DO I RETURN THE TOP TRACKS??
}
});
});
};
exports.doGetTopTrack = doGetTopTrack;
});
在我的“artist.js”文件中”:
require([
'$api/models',
'scripts/get-toptrack'
], function(models, getTopTrack) {
'use strict';
var showRelated = function() {
var artist_properties = ['name', 'popularity','related', 'uri'];
models.Artist
.fromURI('spotify:artist:11FY888Qctoy6YueCpFkXT')
.load(artist_properties)
.done(function(artist){
artist.related.snapshot().done(function(snapshot){
snapshot.loadAll('name').done(function(artists) {
for(var i = 0; i < artists.length; i++){
var u, p, n, t, listItem;
// store artist details
p = artists[i].popularity;
n = artists[i].name;
u = artists[i].uri;
// am I missing something here?
t = getTopTrack.doGetTopTrack(u, 1);
listItem = document.createElement("li");
listItem.innerHTML = "<strong>Name</strong>: " + n.decodeForText() + " | <strong>Popularity: </strong> " + p + " | <strong>Top Track: </strong>" + t;
// undefined name
$('#artistsContainer').append(listItem);
}
});
});
});
};
exports.showArtists = showArtists;
});
在“main.js”文件中,我调用了艺术家函数开始。
require([
'$api/models',
'scripts/artist'
], function(models, initArtist) {
'use strict';
initArtist.showRelated();
});