forked from fredericd/coce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coceclient.js
55 lines (44 loc) · 1.41 KB
/
coceclient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
Usage:
var cc = new CoceClient('http://coceserver.com:8080', 'ol,gb,aws');
cc.fetch(['isbn1','isbn2'], function(isbn, url) {
$('#isbn_'+isbn).html('<img src="'+url+'">');
});
**/
function CoceClient(url, provider) {
var founds = {}; // Private cache for already found ISBN
var notfounds = {}; // ISBN not found in Coce
this.url = url;
this.provider = provider;
this.fetch = function(isbns, cbUpdateUI) {
// First, find ISBNs in client-side cache
var isbntosearch = [];
$.each(isbns, function(i, isbn){
var url = founds[isbn];
if (url) {
cbUpdateUI(isbn, url);
} else if (notfounds[isbn] == undefined) {
notfounds[isbn] = 1;
isbntosearch.push(isbn);
}
});
if (isbntosearch.length == 0) return;
var url = this.url,
provider = this.provider;
$.ajax({
url: url + '/cover?id=' + isbntosearch.join(',') + '&provider=' + provider,
dataType: 'jsonp',
success: function(urlPerISBN) {
$.each(urlPerISBN, function(isbn, url) {
delete notfounds[isbn];
founds[isbn] = url;
cbUpdateUI(isbn, url);
});
}
});
};
this.reset = function() {
found = {};
notfounds = {};
};
};