GUAC-1176: formService must use a provider to be configurable within a config block.

This commit is contained in:
Michael Jumper
2015-06-05 09:16:19 -07:00
parent 48e10ab69a
commit 2b9ff2cd44

View File

@@ -24,68 +24,21 @@
* A service for maintaining form-related metadata and linking that data to * A service for maintaining form-related metadata and linking that data to
* corresponding controllers and templates. * corresponding controllers and templates.
*/ */
angular.module('form').factory('formService', ['$injector', angular.module('form').provider('formService', function formServiceProvider() {
function formService($injector) {
// Required services
var $compile = $injector.get('$compile');
var $http = $injector.get('$http');
var $q = $injector.get('$q');
var $templateCache = $injector.get('$templateCache');
var service = {};
/** /**
* Returns a Promise which resolves with the HTML contents of the * Reference to the provider itself.
* template at the given URL. The template contents will be retrieved from
* the $templateCache if possible.
* *
* @param {String} url * @type formServiceProvider
* The URL of the template to retrieve.
*
* @returns {Promise.<String>}
* A Promise which resolves with the HTML contents of the template at
* the given URL.
*/ */
var templateRequest = function templateRequest(url) { var provider = this;
// Pull template from cache if present
var template = $templateCache.get(url);
if (template)
return $q.when(template);
// Defer retrieval of template
var templateContent = $q.defer();
// Retrieve template manually
$http({
method : 'GET',
url : url,
cache : true
})
// Upon success, resolve promise and update template cache
.success(function templateRetrieved(html) {
$templateCache.put(url, html);
templateContent.resolve(html);
})
// Fail if template cannot be retrieved
.error(function templateError() {
templateContent.reject();
});
// Return promise which will resolve with the retrieved template
return templateContent.promise;
};
/** /**
* Map of all registered field type definitions by name. * Map of all registered field type definitions by name.
* *
* @type Object.<String, FieldType> * @type Object.<String, FieldType>
*/ */
service.fieldTypes = { this.fieldTypes = {
/** /**
* Text field type. * Text field type.
@@ -179,10 +132,68 @@ angular.module('form').factory('formService', ['$injector',
* @param {FieldType} fieldType * @param {FieldType} fieldType
* The field type definition to associate with the given name. * The field type definition to associate with the given name.
*/ */
service.registerFieldType = function registerFieldType(fieldTypeName, fieldType) { this.registerFieldType = function registerFieldType(fieldTypeName, fieldType) {
// Store field type // Store field type
service.fieldTypes[fieldTypeName] = fieldType; provider.fieldTypes[fieldTypeName] = fieldType;
};
// Factory method required by provider
this.$get = ['$injector', function formServiceFactory($injector) {
// Required services
var $compile = $injector.get('$compile');
var $http = $injector.get('$http');
var $q = $injector.get('$q');
var $templateCache = $injector.get('$templateCache');
var service = {};
service.fieldTypes = provider.fieldTypes;
/**
* Returns a Promise which resolves with the HTML contents of the
* template at the given URL. The template contents will be retrieved from
* the $templateCache if possible.
*
* @param {String} url
* The URL of the template to retrieve.
*
* @returns {Promise.<String>}
* A Promise which resolves with the HTML contents of the template at
* the given URL.
*/
var templateRequest = function templateRequest(url) {
// Pull template from cache if present
var template = $templateCache.get(url);
if (template)
return $q.when(template);
// Defer retrieval of template
var templateContent = $q.defer();
// Retrieve template manually
$http({
method : 'GET',
url : url,
cache : true
})
// Upon success, resolve promise and update template cache
.success(function templateRetrieved(html) {
$templateCache.put(url, html);
templateContent.resolve(html);
})
// Fail if template cannot be retrieved
.error(function templateError() {
templateContent.reject();
});
// Return promise which will resolve with the retrieved template
return templateContent.promise;
}; };
@@ -217,7 +228,7 @@ angular.module('form').factory('formService', ['$injector',
service.createFieldElement = function createFieldElement(fieldTypeName, scope) { service.createFieldElement = function createFieldElement(fieldTypeName, scope) {
// Ensure field type is defined // Ensure field type is defined
var fieldType = service.fieldTypes[fieldTypeName]; var fieldType = provider.fieldTypes[fieldTypeName];
if (!fieldType) if (!fieldType)
return $q.reject(); return $q.reject();
@@ -250,4 +261,6 @@ angular.module('form').factory('formService', ['$injector',
return service; return service;
}]); }];
});