Restricture musicbrainz.js.
authorSalvatore Iovene <salvatore@iovene.com>
Mon, 18 Mar 2013 06:56:27 +0000 (08:56 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Mon, 18 Mar 2013 06:56:27 +0000 (08:56 +0200)
examples/hoofbeats/javascripts/musicbrainz.js

index 0b1a751..17de3ab 100644 (file)
 /* vi: set et sw=4 ts=4 si: */
 (function(win, $) {
     var musicbrainz = function() {
-        this._baseUrl = 'http://musicbrainz.org/ws/2/';
-        this._fmtArg = 'fmt=json';
-        this._interval = undefined;
-        this._queue = [];
-        this._url_cache = {};
-
-
-        /* Internal methods to perform XHR requests. */
-
-        this._get = function(request) {
-            var self = this;
-
-            if (request.url in self._url_cache) {
-                request.deferred.resolve(self._url_cache[request.url]);
-            } else {
-                $.getJSON(request.url)
-                .done(function(data, statusText, xhr) {
-                    self._url_cache[request.url] = data;
-                    request.deferred.resolve(data);
-                })
-                .error(function(xhr) {
-                    if (xhr.status === 503) {
-                        self._enqueue(request); // put it back in the queue
-                    }
-                });
-            }
-
-            return request.deferred.promise();
-        };
+        var _priv = {
+            _baseUrl: 'http://musicbrainz.org/ws/2/',
+            _fmtArg: 'fmt=json',
+            _interval: undefined,
+            _queue: [],
+            _url_cache: {},
+
+
+            /* Internal methods to perform XHR requests. */
+
+            _get: function(request) {
+                if (request.url in _priv._url_cache) {
+                    request.deferred.resolve(_priv._url_cache[request.url]);
+                } else {
+                    $.getJSON(request.url)
+                    .done(function(data, statusText, xhr) {
+                        _priv._url_cache[request.url] = data;
+                        request.deferred.resolve(data);
+                    })
+                    .error(function(xhr) {
+                        if (xhr.status === 503) {
+                            _priv._enqueue(request); // put it back in the queue
+                        }
+                    });
+                }
 
+                return request.deferred.promise();
+            },
 
-        /* Internal methods for the handling of the request queue */
 
-        this._startQueue = function() {
-            var self = this;
+            /* Internal methods for the handling of the request queue */
 
-            if (self._interval === undefined) {
-                self._interval = setInterval(function() {
-                    self._dequeue();
-                }, 2000);
-            }
-        };
+            _startQueue: function() {
+                if (_priv._interval === undefined) {
+                    _priv._interval = setInterval(function() {
+                        _priv._dequeue();
+                    }, 2000);
+                }
+            },
 
-        this._stopQueue = function() {
-            clearInterval(this._interval);
-            this._interval = undefined;
-        };
+            _stopQueue: function() {
+                clearInterval(_priv._interval);
+                _priv._interval = undefined;
+            },
 
-        this._enqueue = function(request) {
-            if ($.inArray(request.url, this._queue) < 0) {
-                this._queue.push(request);
-            }
-        };
+            _enqueue: function(request) {
+                if ($.inArray(request.url, _priv._queue) < 0) {
+                    _priv._queue.push(request);
+                }
+            },
 
-        this._dequeue = function() {
-            var request = this._queue.shift();
-            if (request !== undefined) {
-                this._get(request);
+            _dequeue: function() {
+                var request = _priv._queue.shift();
+                if (request !== undefined) {
+                    _priv._get(request);
+                }
             }
-        };
+        }; // _priv
 
 
         /* Public methods */
+        return {
+            getArtist: function(q) {
+                var lookup_url = _priv._baseUrl + 'artist/?query="' + q +
+                                 '"&' + _priv._fmtArg,
+                    lookup_deferred = new $.Deferred(),
+                    returned_deferred = new $.Deferred();
+
+                if (q !== undefined) {
+                    if (_priv._interval === undefined) {
+                        _priv._startQueue();
+                    }
 
-        this.getArtist = function(q) {
-            var self = this,
-                lookup_url = self._baseUrl + 'artist/?query="' + q +
-                             '"&' + self._fmtArg,
-                lookup_deferred = new $.Deferred(),
-                returned_deferred = new $.Deferred();
-
-            if (q !== undefined) {
-                if (self._interval === undefined) {
-                    self._startQueue();
+                    _priv._enqueue({url: lookup_url, deferred: lookup_deferred});
+                    lookup_deferred.done(function(data) {
+                        if (data.count > 0) {
+                            var artist_url = _priv._baseUrl + 'artist/' +
+                                             data.artist[0].id + '?' + _priv._fmtArg,
+                                artist_deferred = new $.Deferred();
+
+                            _priv._get({url: artist_url, deferred: artist_deferred});
+                            artist_deferred.done(function(data) {
+                                returned_deferred.resolve(data);
+                            });
+                        } else {
+                            returned_deferred.reject();
+                        }
+                    });
+                } else {
+                    returned_deferred.reject();
                 }
 
-                self._enqueue({url: lookup_url, deferred: lookup_deferred});
-                lookup_deferred.done(function(data) {
-                    if (data.count > 0) {
-                        var artist_url = self._baseUrl + 'artist/' +
-                                         data.artist[0].id + '?' + self._fmtArg,
-                            artist_deferred = new $.Deferred();
-
-                        self._get({url: artist_url, deferred: artist_deferred});
-                        artist_deferred.done(function(data) {
-                            returned_deferred.resolve(data);
-                        });
-                    } else {
-                        returned_deferred.reject();
-                    }
-                });
-            } else {
-                returned_deferred.reject();
+                return returned_deferred.promise();
             }
-
-            return returned_deferred.promise();
         };
-    }
+    };
 
     win.MusicBrainz = new musicbrainz();
 }(window, jQuery));