From: Salvatore Iovene Date: Thu, 14 Mar 2013 08:52:19 +0000 (+0200) Subject: (hoofbeats) Add code to get one item from the lib. X-Git-Tag: 0.1~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e91644456fc13a505faaeabbf5eb1bba4646141e;p=profile%2Fivi%2Fcowhide.git (hoofbeats) Add code to get one item from the lib. --- diff --git a/examples/hoofbeats/javascripts/app/models/mediaItemModel.js b/examples/hoofbeats/javascripts/app/models/mediaItemModel.js index 222e920..90ec418 100644 --- a/examples/hoofbeats/javascripts/app/models/mediaItemModel.js +++ b/examples/hoofbeats/javascripts/app/models/mediaItemModel.js @@ -24,6 +24,17 @@ }); return items; + }, + + find: function(item_id) { + var item; + + console.log("MediaItem.find: entered."); + app.Store.getMediaItem(item_id).done(function(data) { + item = app.MediaItem.create(data); + }); + + return item; } }); diff --git a/examples/hoofbeats/javascripts/app/store.js b/examples/hoofbeats/javascripts/app/store.js index a66b8a6..49a3d16 100644 --- a/examples/hoofbeats/javascripts/app/store.js +++ b/examples/hoofbeats/javascripts/app/store.js @@ -33,6 +33,7 @@ } }, + // TODO: make the next two functions into one, to be DRY. getMediaItems: function(deferred) { var self = this, d = deferred || new $.Deferred(); @@ -50,6 +51,23 @@ } return d.promise(); + }, + + getMediaItem: function(item_id, deferred) { + var self = this, + d = deferred || new $.DeferreD(); + + console.log("Store.getMediaItem: entered."); + if (self.scanCompleted) { + console.log("Store.getMediaITem: scan is completed, resolving promise."); + d.resolve(app.library.item(item_id); + } else { + // If the scan is not completed, we must be still scanning. + console.log("Store.getMediaItem: scan still pending. Trying again later."); + setTimeout(function() { + return self.getMediaItem(item_id, deferred); + }, 100); + } } }; diff --git a/examples/hoofbeats/javascripts/library.js b/examples/hoofbeats/javascripts/library.js index 8ebb65d..ac4a86a 100644 --- a/examples/hoofbeats/javascripts/library.js +++ b/examples/hoofbeats/javascripts/library.js @@ -4,6 +4,9 @@ // For readability: this.initialized = false; + // Whether items should be resolved on MusicBrainz + this.resolve = false; + this.fetchCount = 100; this.fetchOffset = 0; this.oneTime = false; @@ -25,15 +28,20 @@ "UNION", [this.audioTypeFilter, this.videoTypeFilter]); }; - this.scan = function(count) { + this.scan = function(options) { + var opts = options || {}; + if (opts.resolve !== undefined) { + this.resolve = opts.resolve; + } + this.deferred = new $.Deferred(); this.initialize() this.mediaItems = []; this.fetchOffset = 0; - if (count !== undefined) { - this.fetchCount = count; + if (opts.count !== undefined) { + this.fetchCount = opts.count; this.oneTime = true; } @@ -60,12 +68,14 @@ items.forEach(function(item, index, items) { self.mediaItems.push(item); - win.MusicBrainz.getArtist(item.artists[0]).done(function(data) { - console.log( - "HoofbeatsLibrary.findCB: " + - "item resolved on MusicBrainz: " + - data.name); - }); + if (self.resolve) { + win.MusicBrainz.getArtist(item.artists[0]).done(function(data) { + console.log( + "HoofbeatsLibrary.findCB: " + + "item resolved on MusicBrainz: " + + data.name); + }); + } }); if (items.length == this.fetchCount && !this.oneTime) { @@ -83,6 +93,17 @@ self.deferred.resolve(); } }; + + this.item = function(id) { + var ret; + + this.mediaItems.forEach(function(item, index, items) { + if (item.id == id) + ret = item; + }); + + return ret; + }; }; library.prototype = { diff --git a/examples/hoofbeats/tests/unit/library.js b/examples/hoofbeats/tests/unit/library.js index f2ab261..c165e2b 100644 --- a/examples/hoofbeats/tests/unit/library.js +++ b/examples/hoofbeats/tests/unit/library.js @@ -27,12 +27,34 @@ $(function() { }); }); + test("successful scan without resolving", function() { + var lib = new HoofbeatsLibrary(); + stop(); + lib.scan({resolve: false}).then(function() { + ok(lib.resolve == false, "lib.resolve is false"); + start(); + }); + }); + test("scan with count", function() { var lib = new HoofbeatsLibrary(); stop(); - lib.scan(1).then(function() { + lib.scan({count: 1}).then(function() { ok(lib.size == 1, "there is one item in the library"); start(); }); }); + + test("get one item", function() { + var lib = new HoofbeatsLibrary(), + item_id = 'e7e7023b-54b3-41d5-b4a1-aa24498e0572'; + + stop(); + lib.scan({count: 1}).then(function() { + var item = lib.item(item_id); + ok(item !== undefined, "item was found"); + ok(item.id == item_id, "correct item was found"); + start(); + }); + }); });