Skip to content

Commit

Permalink
DefensiveConfiguration is now a factory
Browse files Browse the repository at this point in the history
  • Loading branch information
wrousseau committed Dec 17, 2015
1 parent 67d970f commit 3be457e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/services/angular-defensive.services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import DefensiveConfiguration from './defensive-configuration';
var moduleName = 'ngDefensive.services';

angular.module(moduleName, [])
.provider('DefensiveConfiguration', DefensiveConfiguration.factory);
.factory('DefensiveConfiguration', DefensiveConfiguration.factory);

export default moduleName;
81 changes: 41 additions & 40 deletions src/services/defensive-configuration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const HTTP = new WeakMap();
const TEMPLATE_CACHE = new WeakMap();

class DefensiveConfiguration {

constructor() {
constructor($http, $templateCache) {
HTTP.set(this, $http);
TEMPLATE_CACHE.set(this, $templateCache);
this.configurations = {};
}

Expand All @@ -27,49 +32,45 @@ class DefensiveConfiguration {
return configuration;
}

$get($http, $templateCache) {
getTemplate(confCase) {
return new Promise(function(resolve) {
if (confCase.hasOwnProperty('template')) {
resolve(confCase.template);
} else if (confCase.hasOwnProperty('templateUrl')) {
HTTP.get(this)
.get(confCase.templateUrl, {
cache: TEMPLATE_CACHE.get(this),
headers: {Accept: 'text/html'}
})
.then(function(response) {
resolve(response.data);
});
}
});
}

getDefensiveCase(configurationName) {
let configurations = this.configurations;
return {
getTemplate(confCase) {
return new Promise(function(resolve) {
if (confCase.hasOwnProperty('template')) {
resolve(confCase.template);
} else if (confCase.hasOwnProperty('templateUrl')) {
$http
.get(confCase.templateUrl, {
cache: $templateCache,
headers: {Accept: 'text/html'}
})
.then(function(response) {
resolve(response.data);
});
}
});
},
getDefensiveCase(configurationName) {
let self = this;
return new Promise(function(resolve, reject) {
if (!configurations.hasOwnProperty(configurationName)) {
return reject(`Configuration ${configurationName} does not exist`);
}
let configuration = configurations[configurationName];
while (configuration.cases.length) {
let confCase = configuration.cases.shift();
if (confCase.check()) {
return self.getTemplate(confCase)
.then(function(template) {
confCase.template = template;
return resolve(confCase);
});
}
}
});
return new Promise(function(resolve, reject) {
if (!configurations.hasOwnProperty(configurationName)) {
return reject(`Configuration ${configurationName} does not exist`);
}
};
let configuration = configurations[configurationName];
while (configuration.cases.length) {
let confCase = configuration.cases.shift();
if (confCase.check()) {
return self.getTemplate(confCase)
.then(function(template) {
confCase.template = template;
return resolve(confCase);
});
}
}
});
}

static factory() {
return new DefensiveConfiguration();
static factory($http, $templateCache) {
return new DefensiveConfiguration($http, $templateCache);
}

}
Expand Down

0 comments on commit 3be457e

Please sign in to comment.