From 5feeec36a8fd8c438ae59cfe2fe4fb08dc15ac96 Mon Sep 17 00:00:00 2001 From: Salvatore Iovene Date: Wed, 6 Feb 2013 08:24:17 +0200 Subject: [PATCH] (hoofbeats) Displaying media items works. --- examples/hoofbeats/index.html | 15 +++++--- examples/hoofbeats/javascripts/app/app.js | 18 +--------- .../app/controllers/artistsController.js | 8 ----- .../app/controllers/mediaItemsController.js | 8 +++++ .../javascripts/app/models/artistModel.js | 40 ---------------------- .../javascripts/app/models/mediaItemModel.js | 25 ++++++++++++++ examples/hoofbeats/javascripts/app/router.js | 32 +++++++++++++---- examples/hoofbeats/javascripts/app/store.js | 29 +++++++++++++--- .../hoofbeats/javascripts/app/views/artistsView.js | 10 ------ .../javascripts/app/views/mediaItemsView.js | 10 ++++++ examples/hoofbeats/javascripts/library.js | 21 +++++++++--- examples/hoofbeats/tests/unit/library.js | 11 +++++- 12 files changed, 129 insertions(+), 98 deletions(-) delete mode 100644 examples/hoofbeats/javascripts/app/controllers/artistsController.js create mode 100644 examples/hoofbeats/javascripts/app/controllers/mediaItemsController.js delete mode 100644 examples/hoofbeats/javascripts/app/models/artistModel.js create mode 100644 examples/hoofbeats/javascripts/app/models/mediaItemModel.js delete mode 100644 examples/hoofbeats/javascripts/app/views/artistsView.js create mode 100644 examples/hoofbeats/javascripts/app/views/mediaItemsView.js diff --git a/examples/hoofbeats/index.html b/examples/hoofbeats/index.html index e84cc8e..d2c5152 100644 --- a/examples/hoofbeats/index.html +++ b/examples/hoofbeats/index.html @@ -22,7 +22,7 @@ @@ -30,7 +30,12 @@ {{outlet library}} - @@ -52,15 +57,15 @@ - + - + - + diff --git a/examples/hoofbeats/javascripts/app/app.js b/examples/hoofbeats/javascripts/app/app.js index 967b310..efb2e4f 100644 --- a/examples/hoofbeats/javascripts/app/app.js +++ b/examples/hoofbeats/javascripts/app/app.js @@ -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 index cf7e6ac..0000000 --- a/examples/hoofbeats/javascripts/app/controllers/artistsController.js +++ /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 index 0000000..62d2ce8 --- /dev/null +++ b/examples/hoofbeats/javascripts/app/controllers/mediaItemsController.js @@ -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 index a15f5e9..0000000 --- a/examples/hoofbeats/javascripts/app/models/artistModel.js +++ /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 index 0000000..78a935d --- /dev/null +++ b/examples/hoofbeats/javascripts/app/models/mediaItemModel.js @@ -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); diff --git a/examples/hoofbeats/javascripts/app/router.js b/examples/hoofbeats/javascripts/app/router.js index 67d9e3b..22c0537 100644 --- a/examples/hoofbeats/javascripts/app/router.js +++ b/examples/hoofbeats/javascripts/app/router.js @@ -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)); diff --git a/examples/hoofbeats/javascripts/app/store.js b/examples/hoofbeats/javascripts/app/store.js index 8bebca5..a66b8a6 100644 --- a/examples/hoofbeats/javascripts/app/store.js +++ b/examples/hoofbeats/javascripts/app/store.js @@ -1,5 +1,5 @@ /* vi: set et sw=4 ts=4 si: */ -(function(app) { +(function(app, $) { var Store = function() { this.reset(); this.scan(); @@ -17,9 +17,14 @@ 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() { @@ -28,11 +33,25 @@ } }, - 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 index 83c2863..0000000 --- a/examples/hoofbeats/javascripts/app/views/artistsView.js +++ /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 index 0000000..c369b2e --- /dev/null +++ b/examples/hoofbeats/javascripts/app/views/mediaItemsView.js @@ -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); diff --git a/examples/hoofbeats/javascripts/library.js b/examples/hoofbeats/javascripts/library.js index 32c97c7..8ebb65d 100644 --- a/examples/hoofbeats/javascripts/library.js +++ b/examples/hoofbeats/javascripts/library.js @@ -6,6 +6,7 @@ this.fetchCount = 100; this.fetchOffset = 0; + this.oneTime = false; this.mediaItems = []; this.initialize = function() { @@ -24,13 +25,18 @@ "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); }; @@ -55,11 +61,14 @@ 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; diff --git a/examples/hoofbeats/tests/unit/library.js b/examples/hoofbeats/tests/unit/library.js index 979bc6e..f2ab261 100644 --- a/examples/hoofbeats/tests/unit/library.js +++ b/examples/hoofbeats/tests/unit/library.js @@ -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(); }); }); -- 2.7.4