Update model interaction

This commit is contained in:
gconsidine
2017-07-26 17:49:05 -04:00
parent 669cf79898
commit 98cf28d9f1
9 changed files with 82 additions and 46 deletions

View File

@@ -31,9 +31,10 @@ function AddCredentialsController (models, $state, strings) {
vm.form.inputs = { vm.form.inputs = {
_get: id => { _get: id => {
let type = credentialType.getById(id); let type = credentialType.graft(id);
type.mergeInputProperties();
return credentialType.mergeInputProperties(type); return type.get('inputs.fields');
}, },
_source: vm.form.credential_type, _source: vm.form.credential_type,
_reference: 'vm.form.inputs', _reference: 'vm.form.inputs',

View File

@@ -56,14 +56,15 @@ function EditCredentialsController (models, $state, $scope, strings) {
vm.form.inputs = { vm.form.inputs = {
_get (id) { _get (id) {
let type = credentialType.getById(id); let type = credentialType.graft(id);
let inputs = credentialType.mergeInputProperties(type);
if (type.id === credential.get('credential_type')) { type.mergeInputProperties();
inputs = credential.assignInputGroupValues(inputs);
if (type.get('id') === credential.get('credential_type')) {
return credential.assignInputGroupValues(type.get('inputs.fields'));
} }
return inputs; return type.get('inputs.fields');
}, },
_source: vm.form.credential_type, _source: vm.form.credential_type,
_reference: 'vm.form.inputs', _reference: 'vm.form.inputs',

View File

@@ -5,7 +5,6 @@ import CredentialsStrings from './credentials.strings'
function CredentialsResolve ($q, $stateParams, Me, Credential, CredentialType, Organization) { function CredentialsResolve ($q, $stateParams, Me, Credential, CredentialType, Organization) {
let id = $stateParams.credential_id; let id = $stateParams.credential_id;
let models;
let promises = { let promises = {
me: new Me('get'), me: new Me('get'),
@@ -22,14 +21,9 @@ function CredentialsResolve ($q, $stateParams, Me, Credential, CredentialType, O
promises.credential = new Credential(['get', 'options'], [id, id]); promises.credential = new Credential(['get', 'options'], [id, id]);
return $q.all(promises) return $q.all(promises)
.then(_models_ => { .then(models => {
models = _models_;
let credentialTypeId = models.credential.get('credential_type'); let credentialTypeId = models.credential.get('credential_type');
models.selectedCredentialType = models.credentialType.graft(credentialTypeId);
return models.credentialType.graft(credentialTypeId);
})
.then(selectedCredentialType => {
models.selectedCredentialType = selectedCredentialType;
return models; return models;
}); });

View File

@@ -87,7 +87,34 @@ function get (keys) {
return this.find('get', keys); return this.find('get', keys);
} }
function set (method, keys, value) {
if (!value) {
value = keys;
keys = method;
method = 'GET';
}
keys = keys.split('.');
if (keys.length === 1) {
model[keys[0]] = value;
} else {
let property = keys.splice(-1);
keys = keys.join('.');
let model = this.find(method, keys)
model[property] = value;
}
}
function match (method, key, value) { function match (method, key, value) {
if(!value) {
value = key;
key = method;
method = 'GET';
}
let model = this.model[method.toUpperCase()]; let model = this.model[method.toUpperCase()];
if (!model) { if (!model) {
@@ -149,20 +176,39 @@ function normalizePath (resource) {
return `${version}${resource}/`; return `${version}${resource}/`;
} }
function getById (id) { function graft (id) {
let item = this.get('results').filter(result => result.id === id); let item = this.get('results').filter(result => result.id === id);
return item ? item[0] : undefined; item = item ? item[0] : undefined;
if (!item) {
return undefined;
}
return new this.Constructor('get', item, true);
}
function create (method, resource, graft) {
this.promise = this.request(method, resource);
if (graft) {
return this;
}
return this.promise
.then(() => this);
} }
function BaseModel (path) { function BaseModel (path) {
this.model = {}; this.model = {};
this.get = get; this.get = get;
this.set = set;
this.options = options; this.options = options;
this.find = find; this.find = find;
this.match = match; this.match = match;
this.normalizePath = normalizePath; this.normalizePath = normalizePath;
this.getById = getById; this.graft = graft;
this.create = create;
this.request = request; this.request = request;
this.http = { this.http = {
get: httpGet.bind(this), get: httpGet.bind(this),

View File

@@ -37,15 +37,15 @@ function clearTypeInputs () {
delete this.model.GET.inputs; delete this.model.GET.inputs;
} }
function CredentialModel (method, resource) { function CredentialModel (method, resource, graft) {
BaseModel.call(this, 'credentials'); BaseModel.call(this, 'credentials');
this.Constructor = CredentialModel;
this.createFormSchema = createFormSchema.bind(this); this.createFormSchema = createFormSchema.bind(this);
this.assignInputGroupValues = assignInputGroupValues.bind(this); this.assignInputGroupValues = assignInputGroupValues.bind(this);
this.clearTypeInputs = clearTypeInputs.bind(this); this.clearTypeInputs = clearTypeInputs.bind(this);
return this.request(method, resource) return this.create(method, resource, graft);
.then(() => this);
} }
function CredentialModelLoader (_BaseModel_ ) { function CredentialModelLoader (_BaseModel_ ) {

View File

@@ -14,33 +14,26 @@ function categorizeByKind () {
})); }));
} }
function mergeInputProperties (type) { function mergeInputProperties () {
return type.inputs.fields.map(field => { let required = this.get('inputs.required');
if (!type.inputs.required || type.inputs.required.indexOf(field.id) === -1) {
field.required = false;
} else {
field.required = true;
}
return field; return this.get('inputs.fields').map((field, i) => {
if (!required || required.indexOf(field.id) === -1) {
this.set(`inputs.fields[${i}].required`, false);
} else {
this.set(`inputs.fields[${i}].required`, true);
}
}); });
} }
function graft (id) { function CredentialTypeModel (method, resource, graft) {
let data = this.getById(id);
return new CredentialTypeModel('get', data);
}
function CredentialTypeModel (method, id) {
BaseModel.call(this, 'credential_types'); BaseModel.call(this, 'credential_types');
this.Constructor = CredentialTypeModel;
this.categorizeByKind = categorizeByKind.bind(this); this.categorizeByKind = categorizeByKind.bind(this);
this.mergeInputProperties = mergeInputProperties.bind(this); this.mergeInputProperties = mergeInputProperties.bind(this);
this.graft = graft.bind(this);
return this.request(method, id) return this.create(method, resource, graft);
.then(() => this);
} }
function CredentialTypeModelLoader (_BaseModel_) { function CredentialTypeModelLoader (_BaseModel_) {

View File

@@ -4,13 +4,13 @@ function getSelf () {
return this.get('results[0]'); return this.get('results[0]');
} }
function MeModel (method) { function MeModel (method, resource, graft) {
BaseModel.call(this, 'me'); BaseModel.call(this, 'me');
this.Constructor = MeModel;
this.getSelf = getSelf.bind(this); this.getSelf = getSelf.bind(this);
return this.request(method) return this.create(method, resource, graft);
.then(() => this);
} }
function MeModelLoader (_BaseModel_) { function MeModelLoader (_BaseModel_) {

View File

@@ -1,10 +1,11 @@
let BaseModel; let BaseModel;
function OrganizationModel (method) { function OrganizationModel (method, resource, graft) {
BaseModel.call(this, 'organizations'); BaseModel.call(this, 'organizations');
return this.request(method) this.Constructor = OrganizationModel;
.then(() => this);
return this.create(method, resource, graft);
} }
function OrganizationModelLoader (_BaseModel_) { function OrganizationModelLoader (_BaseModel_) {

View File

@@ -46,7 +46,7 @@ export default ['$scope', 'Rest', 'CredentialList', 'Prompt', 'ProcessErrors', '
} }
$scope[list.name].forEach(credential => { $scope[list.name].forEach(credential => {
credential.kind = credentialType.getById(credential.credential_type).name; credential.kind = credentialType.match('id', credential.credential_type).name;
}); });
} }