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 = {
_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,
_reference: 'vm.form.inputs',

View File

@ -56,14 +56,15 @@ function EditCredentialsController (models, $state, $scope, strings) {
vm.form.inputs = {
_get (id) {
let type = credentialType.getById(id);
let inputs = credentialType.mergeInputProperties(type);
let type = credentialType.graft(id);
type.mergeInputProperties();
if (type.id === credential.get('credential_type')) {
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,
_reference: 'vm.form.inputs',

View File

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

View File

@ -87,7 +87,34 @@ function 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) {
if(!value) {
value = key;
key = method;
method = 'GET';
}
let model = this.model[method.toUpperCase()];
if (!model) {
@ -149,20 +176,39 @@ function normalizePath (resource) {
return `${version}${resource}/`;
}
function getById (id) {
function graft (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) {
this.model = {};
this.get = get;
this.set = set;
this.options = options;
this.find = find;
this.match = match;
this.normalizePath = normalizePath;
this.getById = getById;
this.graft = graft;
this.create = create;
this.request = request;
this.http = {
get: httpGet.bind(this),

View File

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

View File

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

View File

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

View File

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

View File

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