5 export default Ember.Service.extend({ |
5 export default Ember.Service.extend({ |
6 env: function() { |
6 env: function() { |
7 return Ember.getOwner(this).resolveRegistration('config:environment') |
7 return Ember.getOwner(this).resolveRegistration('config:environment') |
8 }, |
8 }, |
9 _getStoreKey: function(id) { |
9 _getStoreKey: function(id) { |
10 return 'rameau:'+id; |
10 return 'bnf:'+id; |
11 }, |
11 }, |
12 getName: function(id) { |
12 getLabel: function(id) { |
13 |
13 |
|
14 if(!id) { |
|
15 return new Ember.RSVP.Promise(function(resolve/*, reject*/) { |
|
16 resolve(""); |
|
17 }); |
|
18 } |
|
19 |
|
20 var bnfId = id; |
|
21 if(id.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { |
|
22 bnfId = id.slice((constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID).length); |
|
23 } |
|
24 else if (id.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { |
|
25 bnfId = id.slice((constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID).length); |
|
26 } |
|
27 else if (id.startsWith(constants.BNF_ARK_BASE_ID)) { |
|
28 bnfId = id.slice(constants.BNF_ARK_BASE_ID.length); |
|
29 } |
|
30 var labelPromise = null; |
|
31 |
|
32 var storeKey = this._getStoreKey(id); |
|
33 var label = store.get(storeKey); |
|
34 |
|
35 if(!label) { |
|
36 //TODO: handle error !!! |
|
37 labelPromise = this.queryLabel(bnfId) |
|
38 .then( function(response) { |
|
39 return store.set(storeKey, response); |
|
40 }); |
|
41 } |
|
42 else { |
|
43 labelPromise = new Ember.RSVP.Promise(function(resolve/*, reject*/) { |
|
44 resolve(label); |
|
45 }); |
|
46 } |
|
47 return labelPromise; |
|
48 |
|
49 }, |
|
50 |
|
51 // make the query for the name. |
|
52 // return a Promise |
|
53 queryLabel: function(id) { |
|
54 return new Ember.RSVP.Promise(function(resolve, reject) { |
|
55 Ember.$.ajax({ |
|
56 //TODO Configuration for the host ? |
|
57 url: this.env().baseURL.replace(/\/$/,"") + "/api/v1/bnf/"+id, |
|
58 success: function(bnfDoc) { |
|
59 var labels = bnfDoc.bnfids; |
|
60 var expectedId = constants.BNF_ARK_BASE_ID + id; |
|
61 resolve((expectedId in labels)?labels[expectedId]:null); |
|
62 }, |
|
63 error: function(req, status, error) { |
|
64 reject(status + ":" + error); |
|
65 } |
|
66 }); |
|
67 }.bind(this)); |
14 } |
68 } |
15 |
69 |
|
70 |
16 }); |
71 }); |