当您将艺术家拖放到侧边栏中的应用程序时,我会构建一个临时播放列表。每次我将一个新的艺术家拖到我的应用程序中时,它会在之前建立一个新的列表,而不会清除旧的列表。 (注意,这里可能不需要一些代码。)
我的问题:每次将艺术家拖放到我的应用程序中时,如何清除或删除当前构建的播放列表然后构建一个新播放列表?我怀疑它需要在getRelated()中调用吗?
models.application.addEventListener('dropped', sidebarDropEventListener);
function sidebarDropEventListener() {
for(var i = 0; i < models.application.dropped.length; i++){
var draggedItem = models.application.dropped[i];
updateFromDragged(draggedItem.uri);
}
}
function updateFromDragged(droppedUri) {
// If dropped item is an artist
if(droppedUri.indexOf('artist') >= 0) {
getRelated(droppedUri);
} else {
console.warn('Dropped item is not an artist');
}
}
// Build playlist
function buildList(trackURIArray){
var arr = trackURIArray;
models.Playlist
.createTemporary("myTempList_" + new Date().getTime())
.done(function (playlist){
playlist.load("tracks").done(function() {
playlist.tracks.add.apply(playlist.tracks, arr).done(function(){
// Create list
var list = List.forCollection(playlist);
// Populate DOM
$('#playlistContainer').append(list.node);
list.init();
});
});
});
}
// Get Related
function getRelated(artist_uri){
models.Artist
.fromURI(artist_uri)
.load('related','name')
.done(function (artist){
artist.related.snapshot().done(function (snapshot){
snapshot.loadAll().done(function (artists){
var promises = [];
for(var i = 0; i < artists.length; i++){
var promise = getTopTrack(artists[i], 1);
promises.push(promise);
}
models.Promise.join(promises)
.done(function (tracks){
buildList(tracks);
})
.fail(function (tracks){
buildList(tracks);
});
});
});
});
}
答案 0 :(得分:1)
我认为您可以将当前播放列表存储到localstorage,然后在drop函数中将其删除。在buildList:
中有类似的东西localStorage.current_temp_playlist = playlist.uri;
然后在updateFromDragged:
models.Playlist.removeTemporary(models.Playlist.fromURI(localStorage.current_temp_playlist));
您可以单独存储播放列表,updateFromDragged只能访问它。
答案 1 :(得分:1)
好吧,考虑到你的代码结构,你可以通过Playlist.removeTemporary方法[1]每次在buildList函数中删除整个播放列表:
models.Playlist.removeTemporary("myTempList_yyyymmddhhmmss");
或者您可以创建一次播放列表,然后每次都清除播放列表。播放列表有一个名为tracks的属性,它是一个Collection,它有一个名为clear [2]的方法。但是,要执行此操作,您必须首先记住加载tracks属性。这看起来像这样:
models.Playlist.load('name','tracks')
.done(function(loadedPlaylistToClear) {
return loadedPlaylistToClear.tracks.clear();
})
.done(function(clearedPlaylist) {
// add new tracks in here
});
[1] https://developer.spotify.com/docs/apps/api/1.0/api-models-playlist.html#removeTemporary
[2] https://developer.spotify.com/docs/apps/api/1.0/api-models-collection.html#clear
答案 2 :(得分:1)
我最终做的是在顶部创建一个全局变量“tempList”。然后将播放列表存储在我的“buildList”函数中。在“updateFromDragged”中,我只使用“tempList.tracks.clear”来清除存储的播放列表轨道。
我也应该在这里使用“removeTemporary”吗?
var tempList;
// Build playlist
function buildList(trackURIArray){
var arr = trackURIArray;
var date = new Date().getTime();
models.Playlist
// prevents appending new tracks on refresh
.createTemporary("myTempList_" + date)
.done(function (playlist){
// Store created playlist
tempList = playlist;
playlist.load("tracks").done(function() {
playlist.tracks.add.apply(playlist.tracks, arr).done(function(){
// Create list
var list = List.forCollection(playlist, {
style: 'rounded',
layout: 'toplist'
});
// Hide loading
$loading.hide();
// Populate DOM
$('#playlistContainer').append(list.node);
list.init();
});
});
});
function updateFromDragged(droppedUri) {
// Clear out old tracks
tempList.tracks.clear();
// Remove the temporary ones not in use to reduce resource load
models.Playlist.removeTemporary( models.Playlist.fromURI(tempList) );
// If dropped item is an artist
if(droppedUri.indexOf('artist') >= 0) {
getRelated(droppedUri);
} else {
console.warn('Dropped item is not an artist');
}
}