You can use the Toplist.forArtist
function for fetching the top 10 tracks by an artist.
The following code fetches the first 10 top tracks for an artist and renders a list view and a player:
require(['$api/toplists#Toplist', '$views/list#List', '$views/image#Image'],
function(Toplist, List, Image) {
var artist = models.Artist.fromURI('spotify:artist:2qk9voo8llSGYcZ6xrBzKx');
var toplist = Toplist.forArtist(artist);
// fetch the 10 most played tracks
toplist.tracks.snapshot(0, 10).done(function(tracks) {
// create temporary playlist
models.Playlist
.createTemporary('myTempList')
.done(function(playlist){
playlist.load('tracks').done(function(){
for (var i = 0, l = tracks.length; i < l; i++) {
var track = tracks.get(i);
playlist.tracks.add(track);
}
// append a list view
var listWrapper = document.getElementById('list-wrapper');
var list = List.forPlaylist(playlist);
listWrapper.appendChild(list.node);
list.init();
// append a player
// note that you might have a grey placeholder
// see https://stackoverflow.com/questions/17477655
var playerWrapper = document.getElementById('player-wrapper');
var player = Image.forPlaylist(playlist, {player: true});
playerWrapper.appendChild(player.node);
});
});
});
});
The JS code refers to 2 placeholders that will contain the views:
<div id="list-wrapper"></div>
<div id="player-wrapper"></div>
Note that due to a problem with mosaic cover images for temporary playlist you may need to replace the default cover placeholder with another image.