cusejuice
cusejuice

Reputation: 10691

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).

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);
              });
          });
        });
      });
    }

Upvotes: 1

Views: 216

Answers (3)

cusejuice
cusejuice

Reputation: 10691

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.

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');
    }    

  }

Upvotes: 1

merlinc
merlinc

Reputation: 524

Well, given your code structure you could either delete the entire playlist each time inside your buildList function via the Playlist.removeTemporary method[1]:

models.Playlist.removeTemporary("myTempList_yyyymmddhhmmss");

or you could create the playlist once, and then clear it playlist each time. A Playlist has a property called tracks which is a Collection, which has a method called clear[2]. However, to do this, you would have to remember to load the tracks property first. This would look something like this:

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

Upvotes: 1

Thomas
Thomas

Reputation: 3368

I would think you could store the current playlist to localstorage, then erase it in the drop function. Something like this in buildList:

localStorage.current_temp_playlist = playlist.uri;

Then in updateFromDragged:

models.Playlist.removeTemporary(models.Playlist.fromURI(localStorage.current_temp_playlist));

You might separately be able to store the playlist somewhere that updateFromDragged can just access it.

Upvotes: 1

Related Questions