Search code examples
jsonbackbone.jshandlebars.jsspotify

BackboneJS HandlebarsJS Display JSON from Spotify JSON


I want to display related artists on my Backbone View by using Spotifys API. So far so good, I managed to get the API/JSON data loaded, but I cant get it displayed in my Handlebars template yet, I get an empty HTML template and I dont know what I'm doing wrong?!?

Here is my Backbone Collection:

ArtistRelated.Collection = Backbone.Collection.extend({
    url: function() {
        return 'https://api.spotify.com/v1/artists/1HY2Jd0NmPuamShAr6KMms/related-artists';
    },      
    parse: function(artists){
        return artists;
    }           
});

And my Handlebars HTML:

{{#each this}}
  <img src="{{images.url}}" alt="{{name}}">
  <div>
   <h3>{{name}}</h3>
  </div>
{{/each}}

The API I took as an example:

https://api.spotify.com/v1/artists/1HY2Jd0NmPuamShAr6KMms/related-artists

What am I doing wrong?


Solution

  • Ok I solved it myself:

    After adding/changing the parse method of the collection into:

    parse: function(response){
        return response.artists;
    }
    

    and the Handlebars template:

    {{#each this}}
     <img src="{{this.images.[0].url}}" alt="{{this.name}}">
     <div>
       <h3>{{this.name}}</h3>
     </div>
    {{/each}}
    

    it now works! :-)