In the API 1.0 some data is not retrieved automatically. This means that, ideally, you will only fetch the data you need, decreasing data traffic and improving performance. The downside is that you will need to compose that array of tracks plus the album name yourself, fetching the data in an async way.
For each track you want to fetch the name of its album. This is how you would do it:
var maxTracks = 15;
var search = Search.search(searchString);
search.tracks.snapshot(0, maxTracks).done(function(snapshot) {
snapshot.toArray().forEach(function(result) {
result.album.load('name').done(function(album) {
// here you have access to the album.name attribute
});
});
}).fail(function() {
console.error('Error retrieving snapshot');
});
The problem with that approach is that you will the data in a different order than the one in which the tracks appear. If you need to keep the order you can use the Promise
object:
var maxTracks = 15;
var search = Search.search(searchString);
search.tracks.snapshot(0, maxTracks).done(function(snapshot) {
var promises = [];
snapshot.toArray().forEach(function(result) {
promises.push(result.album.load('name'));
});
models.Promise.join(promises)
.done(function(albums) { console.log('Loaded all albums.', albums); })
.fail(function(albums) { console.log('Failed to load at least one album.', albums); });
}).fail(function() {
console.error('Error retrieving snapshot');
});
Using an array of Promise
s will keep the same order as the tracks.