Question

I've been working on a small Spotify app for some time now. I started using the old API (0.x) but now that I want to access the user's playlists, I need to use the library module which is only available throught the API version 1.0

The spotify team even gives a migration guide to do so ! (if you read me: Thanks guys for all this ;) ).

I already have created a few objects (It's a small app so I don't need much more than that), with a function for each one of my needs, like so:

var sp = getSpotifyApi();
var models = require('sp://import/scripts/api/models');
var player =  models.player;
var views = require('sp://import/scripts/api/views');

// in file 'mySpotify.js'
var mySpotify =
{
  playerNextTrack: function()
  {
    player.next();
  },
}

Whenever I need to Skip the current track, I can call mySpotify.playerNextTrack();

But now, with the new API, I need to do things like this (from Spotify doc):

require(['$api/models'], function(models) {
  var player = models.player;
  player.next();
});

My question is simple: how can I include this kind of code into my objects ? How can I give a name to this last "function" ?

As I'm fresh new to JS, I'm probably doing something wrong or understood something the wrong way so feel free to elaborate if you can ;)

Was it helpful?

Solution

I'll start with what I think is the easy answer, just put the require([], function {[all your code]}); around your whole myspotify.js file. That's basically what I have for mine, though I haven't built any of my own objects. Don't think that would matter.

Update Here's a bit more info showing that objects can be used. Is this what you were looking for?

require(['$api/models'], function(models) {
var mySpotify =
{
  playerNextTrack: function()
  {
    models.player.skipToNextTrack();
  },
}

var playtest = document.querySelector('#playtest');
playtest.addEventListener('click', function() { mySpotify.playerNextTrack(); });

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top