From: Salvatore Iovene Date: Mon, 18 Mar 2013 07:37:36 +0000 (+0200) Subject: (hoofbeats) Add support for pluggable resolvers. X-Git-Tag: 0.1~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cfc9fcda3d616a8a74521dfd54e77ce56cfe90c2;p=profile%2Fivi%2Fcowhide.git (hoofbeats) Add support for pluggable resolvers. A resolver is something used to get song data online, e.g. MusicBrainz. --- diff --git a/examples/hoofbeats/index.html b/examples/hoofbeats/index.html index fcf1197..e9cf520 100644 --- a/examples/hoofbeats/index.html +++ b/examples/hoofbeats/index.html @@ -62,7 +62,7 @@ - + diff --git a/examples/hoofbeats/javascripts/library.js b/examples/hoofbeats/javascripts/library.js index e8cec59..ada40ba 100644 --- a/examples/hoofbeats/javascripts/library.js +++ b/examples/hoofbeats/javascripts/library.js @@ -6,12 +6,11 @@ var _priv = { initialized: false, deferred: undefined, - // Whether items should be resolved on MusicBrainz - resolve: false, fetchCount: 100, fetchOffset: 0, oneTime: false, mediaItems: [], + resolveBackend: MusicBrainz, errorCB: function(error) { console.log("HoofbeatsLibrary.errorCB: " + error.name); @@ -22,13 +21,13 @@ findCB: function(items) { items.forEach(function(item, index, items) { _priv.mediaItems.push(item); - if (_priv.resolve) { - win.MusicBrainz.getArtist(item.artists[0]).done( + if (_priv.resolveBackend !== undefined) { + _priv.resolveBackend.getArtist(item.artists[0]).done( function(data) { console.log( "HoofbeatsLibrary.findCB: " + - "item resolved on MusicBrainz: " + - data.name); + "item resolved on " + _priv.resolveBackend.name() + + ": " + data.name); } ); } @@ -53,7 +52,6 @@ // Public stuff - return { initialize: function() { if (_priv.initialized) @@ -73,8 +71,8 @@ scan: function(options) { var opts = options || {}; - if (opts.resolve !== undefined) { - _priv.resolve = opts.resolve; + if (opts.resolveBackend !== MusicBrainz) { + _priv.resolveBackend = opts.resolveBackend; } _priv.deferred = new $.Deferred(); @@ -115,7 +113,7 @@ getInitialized: function() { return _priv.initialized; }, getItems: function() { return _priv.mediaItems; }, getSize: function() { return _priv.mediaItems.length; }, - getResolve: function() {return _priv.resolve; } + getResolve: function() {return _priv.resolveBackend !== undefined; } }; }; diff --git a/examples/hoofbeats/javascripts/resolvers/dummy.js b/examples/hoofbeats/javascripts/resolvers/dummy.js new file mode 100644 index 0000000..2893a9a --- /dev/null +++ b/examples/hoofbeats/javascripts/resolvers/dummy.js @@ -0,0 +1,16 @@ +/* vi: set et sw=4 ts=4 si: */ +(function(win, $) { + var dummyResolver = function() { + return { + name: function() { return "DummyResolver"; }, + + getArtist: function(q) { + return new $.Deferred().resolve({ + name: 'Regina Spektor' + }); + } + }; + }; + + win.DummyResolver = new dummyResolver(); +}(window, jQuery)); diff --git a/examples/hoofbeats/javascripts/musicbrainz.js b/examples/hoofbeats/javascripts/resolvers/musicbrainz.js similarity index 98% rename from examples/hoofbeats/javascripts/musicbrainz.js rename to examples/hoofbeats/javascripts/resolvers/musicbrainz.js index 17de3ab..af9d9ed 100644 --- a/examples/hoofbeats/javascripts/musicbrainz.js +++ b/examples/hoofbeats/javascripts/resolvers/musicbrainz.js @@ -63,6 +63,8 @@ /* Public methods */ return { + name: function() { return "MusicBrainz"; }, + getArtist: function(q) { var lookup_url = _priv._baseUrl + 'artist/?query="' + q + '"&' + _priv._fmtArg, diff --git a/examples/hoofbeats/tests/index.html b/examples/hoofbeats/tests/index.html index 7aaec1f..fab892f 100644 --- a/examples/hoofbeats/tests/index.html +++ b/examples/hoofbeats/tests/index.html @@ -15,12 +15,13 @@ + + - + -
diff --git a/examples/hoofbeats/tests/unit/library.js b/examples/hoofbeats/tests/unit/library.js index e746e96..1a7a7da 100644 --- a/examples/hoofbeats/tests/unit/library.js +++ b/examples/hoofbeats/tests/unit/library.js @@ -14,14 +14,14 @@ $(function() { var lib = new HoofbeatsLibrary(); window.tizen = undefined; raises(function() { - lib.scan(); + lib.scan({resolveBackend: DummyResolver}); }, Error, "scan throws Error"); }); test("successful scan", function() { var lib = new HoofbeatsLibrary(); stop(); - lib.scan().then(function() { + lib.scan({resolveBackend: DummyResolver}).then(function() { ok(lib.getSize() > 0, "there are items in the library"); start(); }); @@ -30,8 +30,8 @@ $(function() { test("successful scan without resolving", function() { var lib = new HoofbeatsLibrary(); stop(); - lib.scan({resolve: false}).then(function() { - ok(lib.getResolve() == false, "lib.resolve is false"); + lib.scan({resolveBackend: undefined}).then(function() { + ok(lib.getResolve() == false, "lib.getResolve() is false"); start(); }); }); @@ -39,7 +39,7 @@ $(function() { test("scan with count", function() { var lib = new HoofbeatsLibrary(); stop(); - lib.scan({count: 1}).then(function() { + lib.scan({count: 1, resolveBackend: DummyResolver}).then(function() { ok(lib.getSize() == 1, "there is one item in the library"); start(); }); @@ -50,7 +50,7 @@ $(function() { item_id = 'e7e7023b-54b3-41d5-b4a1-aa24498e0572'; stop(); - lib.scan({count: 1}).then(function() { + lib.scan({count: 1, resolveBackend: DummyResolver}).then(function() { var item = lib.item(item_id); ok(item !== undefined, "item was found"); ok(item.id == item_id, "correct item was found"); diff --git a/examples/hoofbeats/tests/unit/musicbrainz.js b/examples/hoofbeats/tests/unit/resolvers/musicbrainz.js similarity index 100% rename from examples/hoofbeats/tests/unit/musicbrainz.js rename to examples/hoofbeats/tests/unit/resolvers/musicbrainz.js diff --git a/grunt.js b/grunt.js index 257f4fe..fd30df5 100644 --- a/grunt.js +++ b/grunt.js @@ -38,6 +38,7 @@ module.exports = function(grunt) { 'examples/widget-gallery/javascripts/*.js', 'examples/hoofbeats/javascripts/*.js', + 'examples/hoofbeats/javascripts/resolvers/*.js', 'test/**/*.js' ]