X-Git-Url: http://jaekl.net/gitweb/?p=quanweb.git;a=blobdiff_plain;f=js%2FBooksModel.js;h=75e065194ccdc49602033971208598b9bcde7c64;hp=0c0b915769e0cfcd29501ccceef7740a31ec9eec;hb=222a53b5def154dcc59dbaad98404ea2930e8434;hpb=ea10c37dd0cf10799d4e2c3e265f5e0e108a8023 diff --git a/js/BooksModel.js b/js/BooksModel.js index 0c0b915..75e0651 100644 --- a/js/BooksModel.js +++ b/js/BooksModel.js @@ -24,6 +24,35 @@ var BooksModel = (function() { // ============== // 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); }; @@ -42,13 +71,12 @@ var BooksModel = (function() { } fetch(url, {method:'GET', cache:'default'}) - .then(response => response.json()) - .then((jsonValue) => { - console.log('JSON response for info: ', jsonValue); + .then(function(response) {return response.json();}) + .then(function(jsonValue) { my.cache = jsonValue; notifyAll(); // inform all subscribers that the model has been updated }) - .catch(err => { + .catch(function(err) { var msg = 'Error fetching book details via URL: ' + url + ': ' + err; console.log(msg, err.stack); report(msg);