(hoofbeats) Displaying media items works.
authorSalvatore Iovene <salvatore@iovene.com>
Wed, 6 Feb 2013 06:24:17 +0000 (08:24 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Wed, 6 Feb 2013 06:24:17 +0000 (08:24 +0200)
12 files changed:
examples/hoofbeats/index.html
examples/hoofbeats/javascripts/app/app.js
examples/hoofbeats/javascripts/app/controllers/artistsController.js [deleted file]
examples/hoofbeats/javascripts/app/controllers/mediaItemsController.js [new file with mode: 0644]
examples/hoofbeats/javascripts/app/models/artistModel.js [deleted file]
examples/hoofbeats/javascripts/app/models/mediaItemModel.js [new file with mode: 0644]
examples/hoofbeats/javascripts/app/router.js
examples/hoofbeats/javascripts/app/store.js
examples/hoofbeats/javascripts/app/views/artistsView.js [deleted file]
examples/hoofbeats/javascripts/app/views/mediaItemsView.js [new file with mode: 0644]
examples/hoofbeats/javascripts/library.js
examples/hoofbeats/tests/unit/library.js

index e84cc8e..d2c5152 100644 (file)
@@ -22,7 +22,7 @@
       <div class="navbar navbar-inverse">
         <div class="navbar-inner">
           <ul class="nav">
-            <li><a {{action showArtists href=true}}>Artists</a></li>
+            <li><a {{action showMediaItems href=true}}>Items</a></li>
           </ul>
         </div>
       </div>
       {{outlet library}}
     </script>
 
-    <script type="text/x-handlebars" data-template-name="artists">
+    <script type="text/x-handlebars" data-template-name="mediaItems">
+        <ul class="item-list striped">
+            {{#each item in controller}}
+                <li>{{item.title}}</li>
+            {{/each}}
+        </ul>
     </script>
 
     <!-- the UI framework -->
     <script src="javascripts/app/store.js"></script>
 
     <!-- models -->
-    <script src="javascripts/app/models/artistModel.js"></script>
+    <script src="javascripts/app/models/mediaItemModel.js"></script>
 
     <!-- controllers -->
     <script src="javascripts/app/controllers/applicationController.js">
     </script>
-    <script src="javascripts/app/controllers/artistsController.js"></script>
+    <script src="javascripts/app/controllers/mediaItemsController.js"></script>
 
     <!-- views -->
-    <script src="javascripts/app/views/artistsView.js"></script>
+    <script src="javascripts/app/views/mediaItemsView.js"></script>
 
   </body>
 </html>
index 967b310..efb2e4f 100644 (file)
@@ -3,22 +3,6 @@
     win.Hoofbeats = win.Ember.Application.create({
         VERSION: '0.1',
         rootElement: '#application',
-        initialized: false,
-        init: function() {
-            var self = this;
-
-            if (self.initialized) {
-                return;
-            }
-
-            if (win.HoofbeatsLibrary) {
-                self.library = new win.HoofbeatsLibrary();
-                self.initialized = true;
-            } else {
-                setTimeout(function() {
-                    self.init();
-                }, 100);
-            }
-        }
+        library: null,
     });
 }(window));
diff --git a/examples/hoofbeats/javascripts/app/controllers/artistsController.js b/examples/hoofbeats/javascripts/app/controllers/artistsController.js
deleted file mode 100644 (file)
index cf7e6ac..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-(function(app, Ember, _) {
-    var ArtistsController = Ember.ArrayController.extend({
-        content: [],
-    });
-
-    app.ArtistsController = ArtistsController;
-    app.artistsController = ArtistsController.create();
-})(window.Hoofbeats, window.Ember);
diff --git a/examples/hoofbeats/javascripts/app/controllers/mediaItemsController.js b/examples/hoofbeats/javascripts/app/controllers/mediaItemsController.js
new file mode 100644 (file)
index 0000000..62d2ce8
--- /dev/null
@@ -0,0 +1,8 @@
+(function(app, Ember, _) {
+    var MediaItemsController = Ember.ArrayController.extend({
+        content: [],
+    });
+
+    app.MediaItemsController = MediaItemsController;
+    app.mediaItemsController = MediaItemsController.create();
+})(window.Hoofbeats, window.Ember);
diff --git a/examples/hoofbeats/javascripts/app/models/artistModel.js b/examples/hoofbeats/javascripts/app/models/artistModel.js
deleted file mode 100644 (file)
index a15f5e9..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-(function(app, Ember) {
-    var Artist = Ember.Object.extend({
-        id: null,
-        firstName: null,
-        lastName: null,
-        bandName: null,
-
-        name: function() {
-            var ret = "";
-            if (this.get('firstName') && this.get('lastName')) {
-                ret = "%@, %@".fmt(this.get('lastName', this.get('firstName')));
-                if(this.get('bandName')) {
-                    ret += " (%@)".fmt(this.get('bandName'));
-                }
-            } else {
-                ret = this.get('bandName');
-            }
-
-            return ret;
-        }.property('firstName', 'lastName', 'bandName'),
-
-        isBandOnly: function() {
-            if (this.get('firstName') === null &&
-                this.get('lastName') === null &&
-                this.get('bandName') !== null) {
-                return true;
-            }
-            return false;
-        }.property('firstName', 'lastName', 'bandName')
-    });
-
-
-    Artist.reopenClass({
-        find: function() {
-            return app.Store.getArtists();
-        }
-    });
-
-    app.Artist = Artist;
-})(window.Hoofbeats, window.Ember);
diff --git a/examples/hoofbeats/javascripts/app/models/mediaItemModel.js b/examples/hoofbeats/javascripts/app/models/mediaItemModel.js
new file mode 100644 (file)
index 0000000..78a935d
--- /dev/null
@@ -0,0 +1,25 @@
+(function(app, Ember) {
+    var MediaItem = Ember.Object.extend({
+        id: null,
+        title: null
+    });
+
+
+    MediaItem.reopenClass({
+        findAll: function() {
+            var items = [];
+
+            console.log("MediaItem.findAll: entered.");
+            app.Store.getMediaItems().done(function(data) {
+                data.forEach(function(item) {
+                    console.log("MediaItem.findAll: pushing object.");
+                    items.pushObject(app.MediaItem.create(item));
+                });
+            });
+
+            return items;
+        }
+    });
+
+    app.MediaItem = MediaItem;
+})(window.Hoofbeats, window.Ember);
index 67d9e3b..22c0537 100644 (file)
@@ -1,26 +1,44 @@
 /* vi: set et sw=4 ts=4 si: */
-(function(app, Ember) {
+(function(win, app, Ember) {
     var Router = Ember.Router.extend({
         location: 'none',
 
-        showArtists: Ember.Route.transitionTo('artists'),
+        showMediaItems: Ember.Route.transitionTo('mediaItems'),
+
+        initLibrary: function() {
+            var self = this;
+
+            if (app.library !== null) {
+                return;
+            }
+
+            if (win.HoofbeatsLibrary) {
+                app.library = new win.HoofbeatsLibrary();
+            } else {
+                setTimeout(function() {
+                    self.initLibrary();
+                }, 100);
+            }
+
+        },
 
         root: Ember.Route.extend({
             index: Ember.Route.extend({
                 route: '/',
-                redirectsTo: 'artists'
+                redirectsTo: 'mediaItems'
             }),
 
-            artists: Ember.Route.extend({
-                route: '/artists',
+            mediaItems: Ember.Route.extend({
+                route: '/mediaItems',
                 connectOutlets: function(router) {
                     var controller = router.get('applicationController');
+                    router.initLibrary();
                     controller.connectOutlet(
-                        'library', 'artists', app.Artist.find());
+                        'library', 'mediaItems', app.MediaItem.findAll());
                 }
             })
         })
     });
 
     app.Router = Router;
-}(window.Hoofbeats, window.Ember));
+}(window, window.Hoofbeats, window.Ember));
index 8bebca5..a66b8a6 100644 (file)
@@ -1,5 +1,5 @@
 /* vi: set et sw=4 ts=4 si: */
-(function(app) {
+(function(app, $) {
     var Store = function() {
         this.reset();
         this.scan();
                 return;
             }
 
+            console.log("Store.scan: entered.");
             if (app.library) {
                 app.library.scan().then(function() {
                     self.scanCompleted = true;
+                    console.log(
+                        "Store.scan: completed. " +
+                        app.library.size +
+                        " items in the library.");
                 });
             } else {
                 setTimeout(function() {
             }
         },
 
-        getArtists: function() {
-            var artists = [];
-            return artists;
+        getMediaItems: function(deferred) {
+            var self = this,
+                d = deferred || new $.Deferred();
+
+            console.log("Store.getMediaItems: entered.");
+            if (self.scanCompleted) {
+                console.log("Store.getMediaItems: scan is completed, resolving promise.");
+                deferred.resolve(app.library.items);
+            } else {
+                // If the scan is not completed, we must be still scanning.
+                console.log("Store.getMediaItems: scan still pending. Trying again later.");
+                setTimeout(function() {
+                    return self.getMediaItems(d);
+                }, 100);
+            }
+
+            return d.promise();
         }
     };
 
     app.Store = new Store();
-})(window.Hoofbeats);
+})(window.Hoofbeats, window.jQuery);
diff --git a/examples/hoofbeats/javascripts/app/views/artistsView.js b/examples/hoofbeats/javascripts/app/views/artistsView.js
deleted file mode 100644 (file)
index 83c2863..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-/* vi: set et sw=4 ts=4 si: */
-(function(app, Ember) {
-    var ArtistsView = Ember.View.extend({
-        templateName: 'artists',
-        tagName: 'ul',
-        classNames: ['artists']
-    });
-
-    app.ArtistsView = ArtistsView;
-})(window.Hoofbeats, window.Ember);
diff --git a/examples/hoofbeats/javascripts/app/views/mediaItemsView.js b/examples/hoofbeats/javascripts/app/views/mediaItemsView.js
new file mode 100644 (file)
index 0000000..c369b2e
--- /dev/null
@@ -0,0 +1,10 @@
+/* vi: set et sw=4 ts=4 si: */
+(function(app, Ember) {
+    var MediaItemsView = Ember.View.extend({
+        templateName: 'mediaItems',
+        tagName: 'ul',
+        classNames: ['mediaItems']
+    });
+
+    app.MediaItemsView = MediaItemsView;
+})(window.Hoofbeats, window.Ember);
index 32c97c7..8ebb65d 100644 (file)
@@ -6,6 +6,7 @@
 
         this.fetchCount = 100;
         this.fetchOffset = 0;
+        this.oneTime = false;
         this.mediaItems = [];
 
         this.initialize = function() {
                 "UNION", [this.audioTypeFilter, this.videoTypeFilter]);
         };
 
-        this.scan = function() {
+        this.scan = function(count) {
             this.deferred = new $.Deferred();
             this.initialize()
 
             this.mediaItems = [];
             this.fetchOffset = 0;
 
+            if (count !== undefined) {
+                this.fetchCount = count;
+                this.oneTime = true;
+            }
+
             tizen.content.find(
                 this.findCB.bind(this),
                 this.errorCB.bind(this),
@@ -44,7 +50,7 @@
         };
 
         this.errorCB = function(error) {
-            console.log("Error: " + error.name);
+            console.log("HoofbeatsLibrary.errorCB: " + error.name);
             this.deferred.reject();
             throw new Error(error.name);
         };
             items.forEach(function(item, index, items) {
                 self.mediaItems.push(item);
                 win.MusicBrainz.getArtist(item.artists[0]).done(function(data) {
-                    console.log(data);
+                    console.log(
+                        "HoofbeatsLibrary.findCB: " + 
+                        "item resolved on MusicBrainz: " +
+                        data.name);
                 });
             });
 
-            if (items.length == this.fetchCount) {
+            if (items.length == this.fetchCount && !this.oneTime) {
                 // There *might* be more items.
                 this.fetchOffset += this.fetchCount;
                 tizen.content.find(
@@ -78,7 +87,9 @@
 
     library.prototype = {
         set initialized(value) {this._initialized = value; },
-        get initialized() { return this._initialized; }
+        get initialized() { return this._initialized; },
+        get items() { return this.mediaItems; },
+        get size() { return this.mediaItems.length; }
     };
 
     win.HoofbeatsLibrary = library;
index 979bc6e..f2ab261 100644 (file)
@@ -22,7 +22,16 @@ $(function() {
         var lib = new HoofbeatsLibrary();
         stop();
         lib.scan().then(function() {
-            ok(lib.mediaItems.length > 0, "there are items in the library");
+            ok(lib.size > 0, "there are items in the library");
+            start();
+        });
+    });
+
+    test("scan with count", function() {
+        var lib = new HoofbeatsLibrary();
+        stop();
+        lib.scan(1).then(function() {
+            ok(lib.size == 1, "there is one item in the library");
             start();
         });
     });