Reputation: 426
I am trying to display a list of the current users playlists in the native spotify List.
I've got this to get the users playlist:
require([
'$api/models',
'$views/list#List',
'$api/library#Library'
], function(models, List, Library) {
'use strict';
var UsersLibrary = Library.forCurrentUser();
var test = function()
{
console.log(UsersLibrary.playlists);
var playlists = UsersLibrary.playlists.snapshot().done(function(s){
var length = s.length;
for(var x = 0; x < length; x++)
{
var meta = s._meta[x];
if(!meta.name) continue ; // skip folders
}
});
};
exports.test = test;
});
And i can loop over the playlists no problem. The problem starts when i try to use the spotify List object and create a list. I tried this (and a few variations with the same result):
var test = function()
{
console.log(UsersLibrary.playlists);
var playlists = UsersLibrary.playlists.snapshot().done(function(s){
var list = List.forCollection(s._collection, {"layout":'table'});
});
};
As soon as I call .forCollection I get Uncaught TypeError: Cannot call method 'indexOf' of null
.
After debugging for awhile I found the problem comes when one of the spotify API scripts tries to access the uri
field on the collection which is undefined.
Is there some other way to go about this or something im missing? I was thinking there would be a uri for the users playlists but i couldn't find anything.
Thanks,
-Ken
Upvotes: 0
Views: 285
Reputation: 3279
The collection contains a set of playlists. You can load all of them calling its loadAll
function:
require([
'$api/models',
'$views/list#List',
'$api/library#Library'
], function(models, List, Library) {
'use strict';
var UsersLibrary = Library.forCurrentUser();
var test = function() {
UsersLibrary.playlists.snapshot().done(function(s){
s.loadAll().done(function(playlists){
// playlists is an array of Playlist objects
});
});
};
exports.test = test;
});
If you would like to create a List
for a specific playlist, you would do:
var playlist = playlist[0]; // we pick, for example, the first one
playlists.load('tracks').done(function(p) {
var list = List.forCollection(p.tracks, {"layout":'table'});
document.body.appendChild(list.node);
list.init();
});
Upvotes: 2