-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPLA.js
63 lines (52 loc) · 1.59 KB
/
DPLA.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
55
56
57
58
59
60
61
62
63
if (Meteor.isClient) {
var doSearch = function () {
var searchTerm = document.getElementById("searchInput").value;
var results = document.getElementById("results");
Meteor.call('fetchFromService', searchTerm, function(err, respJson) {
if (err) {
console.log("fetchFromService error: ", err);
}
else {
console.log("term: '" + searchTerm + "', results: ", respJson);
var $results = $(results);
$results.empty();
for (var i = 0; i < respJson.docs.length; i++) {
var doc = respJson.docs[i];
$results.append('<div>' + doc.sourceResource.title + '</div>');
}
}
});
};
Template.DPLASearch.greeting = function () {
return "Welcome to DPLA.";
};
Template.DPLASearch.events({
'click input.search' : function () {
doSearch();
},
'keypress input.searchTerm': function (evt) {
if (evt.which === 13) {
doSearch();
}
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
Meteor.methods({
fetchFromService: function(searchTerm) {
var url = 'http://api.dp.la/v2/items?api_key=08ff0483fd4916b9dafd6f46dc7d2599&q=' + searchTerm;
var result = Meteor.http.get(url);
if (result.statusCode == 200) {
return JSON.parse(result.content);
}
else {
console.log("error fetching: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
}
});
}