+++ /dev/null
-// ==========
-// 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;
-})();