# HG changeset patch # User ymh # Date 1476356472 -7200 # Node ID 04e728651a485306212daa211882bec6d7491244 # Parent 13564bb13ccc9a584b6dafc646cfd40a742b6c5f add reference to backRootUrl in common module diff -r 13564bb13ccc -r 04e728651a48 common/corpus-common-addon/addon/services/resolver-service.js --- a/common/corpus-common-addon/addon/services/resolver-service.js Tue Oct 11 16:39:11 2016 +0200 +++ b/common/corpus-common-addon/addon/services/resolver-service.js Thu Oct 13 13:01:12 2016 +0200 @@ -2,103 +2,115 @@ import _ from 'lodash'; import store from 'corpus-common-addon/utils/store'; +const DEFAULT_DEBOUNCE = 10; + export default Ember.Service.extend({ - promises: null, - queryRegister: null, + promises: null, + queryRegister: null, - storeKeyBase: null, - apiPath: null, - resDocRoot: null, + storeKeyBase: null, + apiPath: null, + resDocRoot: null, - processId: function(id) { return id; }, - - getReturnDictKey: function(id) { return id; }, + processId: function (id) { + return id; + }, - init() { - this._super(...arguments); - this.set('promises', []); - this.set('queryRegister',{}); - this.doQuery = _.debounce(this._doQuery,10); - }, + getReturnDictKey: function (id) { + return id; + }, - env: function() { - return Ember.getOwner(this).resolveRegistration('config:environment'); - }, + init(...args) { + this._super(...args); + this.set('promises', []); + this.set('queryRegister', {}); + this.doQuery = _.debounce(this._doQuery, DEFAULT_DEBOUNCE); + }, + + env: function () { + return Ember.getOwner(this).resolveRegistration('config:environment'); + }, - _getStoreKey: function(id) { - return this.storeKeyBase+':'+id; - }, + _getStoreKey: function (id) { + return this.storeKeyBase + ':' + id; + }, + + _doQuery: function () { + const queryRegister = this.get('queryRegister'); + + this.set('queryRegister', {}); - _doQuery: function() { - var queryRegister = this.get('queryRegister'); - this.set('queryRegister', {}); - var ids = Object.keys(queryRegister).join(','); - if(!ids) { - _.each(queryRegister, (resolve_reject) => { - resolve_reject[0]({}); - }); - } - Ember.$.ajax({ - //TODO Configuration ? - url: this.env().rootURL.replace(/\/$/,'') + this.apiPath +ids, + const ids = Object.keys(queryRegister).join(','); - success: (itemDoc) => { - _.each(queryRegister, (resolve_reject) => { - resolve_reject[0](itemDoc[this.resDocRoot]); - }); - }, - error: (req, status, error) => { - _.each(queryRegister, (resolve_reject) => { - resolve_reject[1](status + ':' + error); - }); - - } - }); - }, + if (!ids) { + _.each(queryRegister, resolveReject=> { + resolveReject[0]({}); + }); + } + Ember.$.ajax({ + // TODO Configuration ? + url: this.env().APP.backRootURL.replace(/\/$/, '') + this.apiPath + ids, - // make the query for the name. - // return a Promise - queryName: function(id) { - var queryRegister = this.get('queryRegister'); - return new Ember.RSVP.Promise(function(resolve, reject) { - queryRegister[id] = [resolve, reject]; - this.doQuery(this); - }.bind(this)); - }, + success: itemDoc=> { + _.each(queryRegister, resolveReject=> { + resolveReject[0](itemDoc[this.resDocRoot]); + }); + }, + error: (req, status, error)=> { + _.each(queryRegister, resolveReject=> { + resolveReject[1](status + ':' + error); + }); - getName: function(id) { - - if(!id) { - return new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(''); - }); - } - - var objectId = this.processId(id); + } + }); + }, - var namePromise = null; - - var storeKey = this._getStoreKey(id); - var name = store.get(storeKey); + // make the query for the name. + // return a Promise + queryName: function (id) { + const queryRegister = this.get('queryRegister'); - if( name !== null) { - namePromise = new Ember.RSVP.Promise(function(resolve/*, reject*/) { - resolve(name); - }); - } else if ( storeKey in this.get('promises')) { - namePromise = this.get('promises')[storeKey]; - } else { - //TODO: handle error !!! - namePromise = this.queryName(objectId) - .then(function(names) { - delete this.get('promises')[storeKey]; - return store.set(storeKey, names[this.getReturnDictKey(objectId)]); - }.bind(this)); - this.get('promises')[storeKey] = namePromise; - } - return namePromise; + return new Ember.RSVP.Promise(function (resolve, reject) { + queryRegister[id] = [resolve, reject]; + this.doQuery(this); + }.bind(this)); + }, + + getName: function (id) { + + if (!id) { + return new Ember.RSVP.Promise(function (resolve/* , reject*/) { + resolve(''); + }); } + const objectId = this.processId(id); + + let namePromise = null; + + const storeKey = this._getStoreKey(id); + const name = store.get(storeKey); + + if (name !== null) { + namePromise = new Ember.RSVP.Promise(function (resolve/* , reject*/) { + resolve(name); + }); + } else if (storeKey in this.get('promises')) { + namePromise = this.get('promises')[storeKey]; + } else { + // handle error !!! + namePromise = this.queryName(objectId) + .then(function (names) { + Reflect.deleteProperty(this.get('promises'), storeKey); + + return store.set(storeKey, names[this.getReturnDictKey(objectId)]); + }.bind(this)); + this.get('promises')[storeKey] = namePromise; + } + + return namePromise; + } + }); diff -r 13564bb13ccc -r 04e728651a48 common/corpus-common-addon/app/services/bnf-resolver.js --- a/common/corpus-common-addon/app/services/bnf-resolver.js Tue Oct 11 16:39:11 2016 +0200 +++ b/common/corpus-common-addon/app/services/bnf-resolver.js Thu Oct 13 13:01:12 2016 +0200 @@ -4,31 +4,34 @@ export default ResolverService.extend({ - storeKeyBase: 'bnf', - apiPath: '/api/v1/resolvers/bnf/', - resDocRoot: 'bnfids', + storeKeyBase: 'bnf', + apiPath: '/api/v1/resolvers/bnf/', + resDocRoot: 'bnfids', - init() { - this._super(...arguments); - console.log("BNF", this); - }, + init(...args) { + this._super(...args); + }, + + processId: function (id) { + let bnfId = id; - processId: function(id) { - var bnfId = id; - if(id.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { - bnfId = id.slice((constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID).length); - } - else if (id.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { - bnfId = id.slice((constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID).length); - } - else if (id.startsWith(constants.BNF_ARK_BASE_ID)) { - bnfId = id.slice(constants.BNF_ARK_BASE_ID.length); - } - return bnfId; - }, + if (id.startsWith(constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID)) { + bnfId = id.slice((constants.BNF_BASE_URL + constants.BNF_ARK_BASE_ID).length); + } else if (id.startsWith(constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID)) { + bnfId = id.slice((constants.BNF_ARK_BASE_URL + constants.BNF_ARK_BASE_ID).length); + } else if (id.startsWith(constants.BNF_ARK_BASE_ID)) { + bnfId = id.slice(constants.BNF_ARK_BASE_ID.length); + } - getReturnDictKey: function(id) { return constants.BNF_ARK_BASE_ID+id; }, + return bnfId; + }, - getLabel: function(id) { return this.getName(id); } + getReturnDictKey: function (id) { + return constants.BNF_ARK_BASE_ID + id; + }, + + getLabel: function (id) { + return this.getName(id); + } }); diff -r 13564bb13ccc -r 04e728651a48 common/corpus-common-addon/tests/dummy/config/environment.js --- a/common/corpus-common-addon/tests/dummy/config/environment.js Tue Oct 11 16:39:11 2016 +0200 +++ b/common/corpus-common-addon/tests/dummy/config/environment.js Thu Oct 13 13:01:12 2016 +0200 @@ -16,6 +16,7 @@ APP: { // Here you can pass flags/options to your application instance // when it is created + backRootURL: '/' } }; @@ -30,6 +31,7 @@ if (environment === 'test') { // Testem prefers this... ENV.rootURL = '/'; + ENV.APP.backRootURL = '/'; ENV.locationType = 'none'; // keep test console output quieter