Spotify 清除播放列表拖放 [英] Spotify Clear Playlist Drag and Drop
问题描述
当您将艺术家拖放到我的侧边栏中的应用中时,我会构建一个临时播放列表.每次我将新艺术家拖入我的应用程序时,它都会在前一个之后构建一个新列表,而不会清除旧列表.(注意这里缺少一些可能不需要的代码).
When you drag/drop an Artist into my app in the sidebar, I build a temporary playlist. Every time I drag a new Artist into my app, it builds a new list after the previous, WITHOUT clearing out the old one. (Note there is some code missing from here that is probably not needed).
我的问题:我如何清除或删除当前构建的播放列表然后构建一个新的,每次我将一个艺术家拖放到我的应用程序中?我怀疑它需要在 getRelated() 中调用?
My question: how do I clear out or remove the current built playlist THEN build a new one, every time I drag/drop an Artist into my app? I suspect it would need to be called inside 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);
});
});
});
});
}
推荐答案
我最终做的是在顶部创建一个全局变量tempList".然后将播放列表存储在我的buildList"函数中.在updateFromDragged"中,我只使用tempList.tracks.clear"来清除存储的播放列表曲目.
What I ended up doing was creating a global variable "tempList" at the top. Then storing the playlist inside my "buildList" function. Inside "updateFromDragged," I just use "tempList.tracks.clear" to clear out the stored playlist tracks.
我也应该在这里使用removeTemporary"吗?
Should I use "removeTemporary" here as well?
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');
}
}
这篇关于Spotify 清除播放列表拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!