Add form submission, validation, rejection messaging

This commit is contained in:
gconsidine
2017-05-23 16:38:58 -04:00
parent c41dff7996
commit 19fa782fb4
21 changed files with 252 additions and 121 deletions

View File

@@ -1,49 +1,71 @@
let $resource;
function get() {
return $resource(this.path).get().$promise
.then(response => {
this.model.data = response;
});
}
function options () {
let actions = {
options: {
method: 'OPTIONS'
}
};
return $resource(this.path, null, actions).options().$promise
.then(response => {
this.model.options = response;
});
}
function getPostOptions (name) {
return this.model.options.actions.POST[name];
}
function normalizePath (resource) {
let version = '/api/v2/';
return `${version}${resource}/`;
}
function BaseModel (_$resource_) {
$resource = _$resource_;
function BaseModel ($http) {
return function extend (path) {
this.get = get;
this.options = options;
this.getPostOptions = getPostOptions;
this.normalizePath = normalizePath;
this.get = () => {
let request = {
method: 'GET',
url: this.path
};
return $http(request)
.then(response => {
this.model.get = response;
});
};
this.post = data => {
let request = {
method: 'POST',
url: this.path,
data,
};
return $http(request)
.then(response => {
this.model.post = response;
});
};
this.options = () => {
let request = {
method: 'OPTIONS',
url: this.path
};
return $http(request)
.then(response => {
this.model.options = response;
});
};
this.getOptions = (method, key) => {
if (!method) {
return this.model.options.data;
}
method = method.toUpperCase();
if (method && !key) {
return this.model.options.data.actions[method];
}
if (method && key) {
return this.model.options.data.actions[method][key];
}
return null;
};
this.normalizePath = resource => {
let version = '/api/v2/';
return `${version}${resource}/`;
};
this.model = {};
this.path = this.normalizePath(path);
};
}
BaseModel.$inject = ['$resource'];
BaseModel.$inject = ['$http'];
export default BaseModel;

View File

@@ -1,7 +1,23 @@
function CredentialModel (BaseModel) {
function CredentialModel (BaseModel, CredentialTypeModel) {
BaseModel.call(this, 'credentials');
this.createFormSchema = (type, config) => {
let schema = Object.assign({}, this.getOptions(type));
if (config && config.omit) {
config.omit.forEach(key => {
delete schema[key];
});
}
for (let key in schema) {
schema[key].id = key;
}
return schema;
};
}
CredentialModel.$inject = ['BaseModel'];
CredentialModel.$inject = ['BaseModel', 'CredentialTypeModel'];
export default CredentialModel;

View File

@@ -4,7 +4,7 @@ function CredentialTypeModel (BaseModel) {
this.categorizeByKind = () => {
let group = {};
this.model.data.results.forEach(result => {
this.model.get.data.results.forEach(result => {
group[result.kind] = group[result.kind] || [];
group[result.kind].push(result);
});
@@ -16,7 +16,7 @@ function CredentialTypeModel (BaseModel) {
};
this.getTypeFromName = name => {
let type = this.model.data.results.filter(result => result.name === name);
let type = this.model.get.data.results.filter(result => result.name === name);
if (!type.length) {
return null;
@@ -36,6 +36,10 @@ function CredentialTypeModel (BaseModel) {
return field;
});
};
this.getResults = () => {
return this.model.get.data.results;
};
}
CredentialTypeModel.$inject = ['BaseModel'];

View File

@@ -2,15 +2,8 @@ import Base from './Base';
import Credential from './Credential';
import CredentialType from './CredentialType';
function config ($resourceProvider) {
$resourceProvider.defaults.stripTrailingSlashes = false;
}
config.$inject = ['$resourceProvider'];
angular
.module('at.lib.models', [])
.config(config)
.service('BaseModel', Base)
.service('CredentialModel', Credential)
.service('CredentialTypeModel', CredentialType);