X-Git-Url: http://jaekl.net/gitweb/?p=quanweb.git;a=blobdiff_plain;f=js%2FBooksModel.js;fp=js%2FBooksModel.js;h=0000000000000000000000000000000000000000;hp=75e065194ccdc49602033971208598b9bcde7c64;hb=13efcb823cde095d5562ac061ef5a859d91c0f70;hpb=222a53b5def154dcc59dbaad98404ea2930e8434 diff --git a/js/BooksModel.js b/js/BooksModel.js deleted file mode 100644 index 75e0651..0000000 --- a/js/BooksModel.js +++ /dev/null @@ -1,96 +0,0 @@ -// ========== -// BooksModel - -var BooksModel = (function() { - - var my = {}; - - // ================= - // Private variables - - var listeners = []; - - // ================ - // Public variables - - my.cache = []; - my.count = 0, - my.first = 0, - my.ids = [], - my.last = (-1), - my.map = {}, // map from book.Id to index into cache[] - my.pageSize = 20; - - // ============== - // Public methods - - my.adjustPos = function(setting) { - - var value = parseInt(setting); - - var prev = { - first: my.first, - last: my.last, - }; - - var maxFirst = Math.max(0, my.count - my.pageSize); - - if (value < 0) { - my.first = 0; - } else if (value > maxFirst) { - my.first = maxFirst; - } else { - my.first = value; - } - - my.last = my.first + my.pageSize - 1; - if (my.last >= my.count) { - my.last = my.count - 1; - } - - if (prev.first !== my.first || prev.last !== my.last) { - my.refreshData(); - } - }; - - my.listen = function(subscriber) { - listeners.push(subscriber); - }; - - my.refreshData = function () { - var i; - var url = '/info/?ids='; - my.map = {}; - for (i = my.first; i <= my.last; ++i) { - if (i > my.first) { - url += ','; - } - var id = my.ids[i]; - url += id; - my.map[id] = i - my.first; - } - - fetch(url, {method:'GET', cache:'default'}) - .then(function(response) {return response.json();}) - .then(function(jsonValue) { - my.cache = jsonValue; - notifyAll(); // inform all subscribers that the model has been updated - }) - .catch(function(err) { - var msg = 'Error fetching book details via URL: ' + url + ': ' + err; - console.log(msg, err.stack); - report(msg); - }); - }; - - // =============== - // Private methods - - function notifyAll() { - for (var i in listeners) { - listeners[i].notify(); - } - } - - return my; -})();