4 var BooksModel = (function() {
17 my.count = 0, // number of books available to be paged through
18 my.first = 0, // first book to be displayed in current page (0-indexed)
20 my.last = (-1), // last book to be displayed in the current page (0-indexed)
21 my.map = {}, // map from book.Id to index into cache[]
27 my.adjustPos = function(setting) {
29 var value = parseInt(setting);
36 var maxFirst = Math.max(0, my.count - my.pageSize);
40 } else if (value > maxFirst) {
46 my.last = my.first + my.pageSize - 1;
47 if (my.last >= my.count) {
48 my.last = my.count - 1;
51 if (prev.first !== my.first || prev.last !== my.last) {
56 my.listen = function(subscriber) {
57 listeners.push(subscriber);
60 my.refreshData = function () {
62 var url = '/info/?ids=';
64 for (i = my.first; i <= my.last; ++i) {
70 my.map[id] = i - my.first;
73 fetch(url, {method:'GET', cache:'default'})
74 .then(function(response) {return response.json();})
75 .then(function(jsonValue) {
77 notifyAll(); // inform all subscribers that the model has been updated
79 .catch(function(err) {
80 var msg = 'Error fetching book details via URL: ' + url + ': ' + err;
81 console.log(msg, err.stack);
89 function notifyAll() {
90 for (var i in listeners) {
91 listeners[i].notify();