-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-people.js
59 lines (51 loc) · 1.18 KB
/
api-people.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
function Person(init) {
fill(this,init,[
'id',
'label',
'gender',
'pic'
]);
}
Person.prototype = Object.create({
// Loading a person's data
//
Load: function() {
var self = this;
return request("GET", ["people",this.id]).then(function(data) {
Person.call(self,data);
return Person.Cache(self);
});
}
});
// Searching for people by name
//
Person.Search = function(q, params) {
params = params || {};
params.q = q;
return request("GET", "people/search", keep(params,[ "q", "limit" ])).then(function(data) {
return data.list.map(Person.Cache);
});
};
// Listing all people in the database
//
Person.List = function(params) {
params = params || {};
return request("GET", "people", keep(params,[ "limit", "offset" ])).then(function(data) {
return data.list.map(Person.Cache);
});
};
// Caching people by their identifier
//
Person.Cache = function(init) {
var p = new Person(init);
person_cache[p.id] = p;
return p;
};
var person_cache = {};
// Try loading a person from cache
//
Person.Get = function(id) {
if (id in person_cache)
return promised(person_cache[id]);
return new Person(id).Load();
};