Last.fm Recent Tracks with AngularJS

AngularJS is an MVC framework for javascript. It is best suited for round-tripping resources with a RESTful service. In this post, I will simply get a list of recent tracks from Last.fm, treating each track as a resource. The Last.fm API is documented here. There is no round-tripping in this example. I’ll also be utilizing Twitter Bootstrap for layout.

To start, create the AngularJS application and a controller for recent tracks. Since this is a simple application, there are no routes and views. The controller will be invoked inline on the main page. Routes and views are easily added once you get a handle on the framework.

'use strict';
 
var LastFmApp = angular.module('lastfm-app', []);
 
LastFmApp.controller('RecentTracksController',
    function RecentTracksController($scope, $http) {
        var url = 'http://ws.audioscrobbler.com/2.0/';
        var params = {
            method: 'user.getrecenttracks',
            api_key: 'your_api_key',
            limit: 12,
            user: 'user_name',
            format: 'json'
        };
        $http.get(url, { params: params })
            .success(function (data) {
                $scope.songs = data.recenttracks.track;
            })
            .error(function (data, status) {
                console.log(data || "Request failed");
                console.log(status);
            });       
    });

The HTML (simplified for brevity).

<!DOCTYPE html>
<html lang="en" ng-app="lastfm-app">
<head>
    <meta charset="utf-8">
    <title>Recent Tracks</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="lib/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="lib/bootstrap/css/bootstrap-responsive.min.css">
</head>
<body>
    <div class="container" ng-controller="RecentTracksController">
        <ul class="thumbnails">
            <li class="span4" ng-repeat="song in songs">
                <a class="thumbnail" href="{{song.url}}" title="{{song.artist['#text']}} - {{song.name}}" style="display: block;">
                    <div class="media">
                        <div class="pull-left" href="#">
                            <img class="media-object" src="{{song.image[2]['#text']}}">
                        </div>
                        <div class="media-body">
                            <strong>{{song.artist['#text']}}</strong><br />
                            {{song.name}}<br />
                            <em>{{song.date['#text']}}</em>
                        </div>
                    </div>
                </a>
            </li>
        </ul>
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="lib/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
    <script src="js/main.js"></script>
</body>
</html>

The resulting grid will look something like this:

Recent Tracks