(hoofbeats) Add code to get one item from the lib.
authorSalvatore Iovene <salvatore@iovene.com>
Thu, 14 Mar 2013 08:52:19 +0000 (10:52 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Thu, 14 Mar 2013 08:52:19 +0000 (10:52 +0200)
examples/hoofbeats/javascripts/app/models/mediaItemModel.js
examples/hoofbeats/javascripts/app/store.js
examples/hoofbeats/javascripts/library.js
examples/hoofbeats/tests/unit/library.js

index 222e920..90ec418 100644 (file)
             });
 
             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;
         }
     });
 
index a66b8a6..49a3d16 100644 (file)
@@ -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();
             }
 
             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);
+            }
         }
     };
 
index 8ebb65d..ac4a86a 100644 (file)
@@ -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;
                 "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;
             }
 
 
             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) {
                 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 = {
index f2ab261..c165e2b 100644 (file)
@@ -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();
+        });
+    });
 });