(hoofbeats) Reorg library code.
authorSalvatore Iovene <salvatore@iovene.com>
Thu, 14 Mar 2013 09:15:53 +0000 (11:15 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Thu, 14 Mar 2013 09:15:53 +0000 (11:15 +0200)
examples/hoofbeats/javascripts/library.js
examples/hoofbeats/tests/unit/library.js

index ac4a86a..85dbbec 100644 (file)
 /* vi: set et sw=4 ts=4 si: */
 (function(win, $) {
     var library = function() {
-        // For readability:
-        this.initialized = false;
-
-        // Whether items should be resolved on MusicBrainz
-        this.resolve = false;
-
-        this.fetchCount = 100;
-        this.fetchOffset = 0;
-        this.oneTime = false;
-        this.mediaItems = [];
-
-        this.initialize = function() {
-            if (this.initialized)
-                return;
-
-            if (win.tizen === undefined) {
-                throw Error("You need the Tizen web API  to run Hoofbeats.");
+        // Private stuff
+
+        var p = {
+            initialized: false,
+            deferred: undefined,
+            // Whether items should be resolved on MusicBrainz
+            resolve: false,
+            fetchCount: 100,
+            fetchOffset: 0,
+            oneTime: false,
+            mediaItems: [],
+
+            errorCB: function(error) {
+                console.log("HoofbeatsLibrary.errorCB: " + error.name);
+                this.deferred.reject();
+                throw new Error(error.name);
+            },
+
+            findCB: function(items) {
+                items.forEach(function(item, index, items) {
+                    p.mediaItems.push(item);
+                    if (p.resolve) {
+                        win.MusicBrainz.getArtist(item.artists[0]).done(
+                            function(data) {
+                                console.log(
+                                    "HoofbeatsLibrary.findCB: " +
+                                    "item resolved on MusicBrainz: " +
+                                    data.name);
+                            }
+                        );
+                    }
+                });
+
+                if (items.length == p.fetchCount && !p.oneTime) {
+                    // There *might* be more items.
+                    p.fetchOffset += p.fetchCount;
+                    tizen.content.find(
+                        p.findCB.bind(this),
+                        p.errorCB.bind(this),
+                        null,
+                        p.typeFilter,
+                        null,
+                        p.fetchCount,
+                        p.fetchOffset);
+                } else {
+                    p.deferred.resolve();
+                }
             }
+        }; // p
 
-            this.audioTypeFilter = new tizen.AttributeFilter(
-                "type", "EXACTLY", "AUDIO");
-            this.videoTypeFilter = new tizen.AttributeFilter(
-                "type", "EXACTLY", "VIDEO");
-            this.typeFilter = new tizen.CompositeFilter(
-                "UNION", [this.audioTypeFilter, this.videoTypeFilter]);
-        };
-
-        this.scan = function(options) {
-            var opts = options || {};
-            if (opts.resolve !== undefined) {
-                this.resolve = opts.resolve;
-            }
 
-            this.deferred = new $.Deferred();
-            this.initialize()
+        // Public stuff
 
-            this.mediaItems = [];
-            this.fetchOffset = 0;
+        return {
+            initialize: function() {
+                if (p.initialized)
+                    return;
 
-            if (opts.count !== undefined) {
-                this.fetchCount = opts.count;
-                this.oneTime = true;
-            }
+                if (win.tizen === undefined) {
+                    throw Error("You need the Tizen web API  to run Hoofbeats.");
+                }
 
-            tizen.content.find(
-                this.findCB.bind(this),
-                this.errorCB.bind(this),
-                null,
-                this.typeFilter,
-                null,
-                this.fetchCount,
-                this.fetchOffset);
+                p.audioTypeFilter = new tizen.AttributeFilter(
+                    "type", "EXACTLY", "AUDIO");
+                p.videoTypeFilter = new tizen.AttributeFilter(
+                    "type", "EXACTLY", "VIDEO");
+                p.typeFilter = new tizen.CompositeFilter(
+                    "UNION", [p.audioTypeFilter, p.videoTypeFilter]);
+            },
+
+            scan: function(options) {
+                var opts = options || {};
+                if (opts.resolve !== undefined) {
+                    p.resolve = opts.resolve;
+                }
 
-            return this.deferred.promise();
-        };
+                p.deferred = new $.Deferred();
+                this.initialize();
 
-        this.errorCB = function(error) {
-            console.log("HoofbeatsLibrary.errorCB: " + error.name);
-            this.deferred.reject();
-            throw new Error(error.name);
-        };
+                p.mediaItems = [];
+                p.fetchOffset = 0;
 
-        this.findCB = function(items) {
-            var self = this;
-
-            items.forEach(function(item, index, items) {
-                self.mediaItems.push(item);
-                if (self.resolve) {
-                    win.MusicBrainz.getArtist(item.artists[0]).done(function(data) {
-                        console.log(
-                            "HoofbeatsLibrary.findCB: " +
-                            "item resolved on MusicBrainz: " +
-                            data.name);
-                    });
+                if (opts.count !== undefined) {
+                    p.fetchCount = opts.count;
+                    p.oneTime = true;
                 }
-            });
 
-            if (items.length == this.fetchCount && !this.oneTime) {
-                // There *might* be more items.
-                this.fetchOffset += this.fetchCount;
                 tizen.content.find(
-                    this.findCB.bind(this),
-                    this.errorCB.bind(this),
+                    p.findCB.bind(this),
+                    p.errorCB.bind(this),
                     null,
-                    this.typeFilter,
+                    p.typeFilter,
                     null,
-                    this.fetchCount,
-                    this.fetchOffset);
-            } else {
-                self.deferred.resolve();
-            }
-        };
+                    p.fetchCount,
+                    p.fetchOffset);
 
-        this.item = function(id) {
-            var ret;
+                return p.deferred.promise();
+            },
 
-            this.mediaItems.forEach(function(item, index, items) {
-                if (item.id == id)
-                    ret = item;
-            });
+            item: function(id) {
+                var ret;
 
-            return ret;
-        };
-    };
+                p.mediaItems.forEach(function(item, index, items) {
+                    if (item.id == id)
+                        ret = item;
+                });
 
-    library.prototype = {
-        set initialized(value) {this._initialized = value; },
-        get initialized() { return this._initialized; },
-        get items() { return this.mediaItems; },
-        get size() { return this.mediaItems.length; }
+                return ret;
+            },
+
+            // Some simple getters
+            getInitialized: function() { return p.initialized; },
+            getItems: function() { return p.mediaItems; },
+            getSize: function() { return p.mediaItems.length; },
+            getResolve: function() {return p.resolve; }
+        };
     };
 
     win.HoofbeatsLibrary = library;
index c165e2b..e746e96 100644 (file)
@@ -22,7 +22,7 @@ $(function() {
         var lib = new HoofbeatsLibrary();
         stop();
         lib.scan().then(function() {
-            ok(lib.size > 0, "there are items in the library");
+            ok(lib.getSize() > 0, "there are items in the library");
             start();
         });
     });
@@ -31,7 +31,7 @@ $(function() {
         var lib = new HoofbeatsLibrary();
         stop();
         lib.scan({resolve: false}).then(function() {
-            ok(lib.resolve == false, "lib.resolve is false");
+            ok(lib.getResolve() == false, "lib.resolve is false");
             start();
         });
     });
@@ -40,7 +40,7 @@ $(function() {
         var lib = new HoofbeatsLibrary();
         stop();
         lib.scan({count: 1}).then(function() {
-            ok(lib.size == 1, "there is one item in the library");
+            ok(lib.getSize() == 1, "there is one item in the library");
             start();
         });
     });