aligning frontend attributes API with the backend one

This commit is contained in:
zadam 2019-12-01 12:22:22 +01:00
parent 295af1f43e
commit 6e83980784

View File

@ -154,20 +154,27 @@ class NoteShort {
} }
/** /**
* @param {string} [name] - attribute name to filter * @param {string} [type] - (optional) attribute type to filter
* @returns {Promise<Attribute[]>} * @param {string} [name] - (optional) attribute name to filter
* @returns {Promise<Attribute[]>} all note's attributes, including inherited ones
*/ */
async getAttributes(name) { async getAttributes(type, name) {
if (!this.attributeCache) { if (!this.__attributeCache) {
this.attributeCache = (await server.get('notes/' + this.noteId + '/attributes')) this.__attributeCache = (await server.get('notes/' + this.noteId + '/attributes'))
.map(attrRow => new Attribute(this.treeCache, attrRow)); .map(attrRow => new Attribute(this.treeCache, attrRow));
} }
if (name) { if (type && name) {
return this.attributeCache.filter(attr => attr.name === name); return this.__attributeCache.filter(attr => attr.type === type && attr.name === name);
}
else if (type) {
return this.__attributeCache.filter(attr => attr.type === type);
}
else if (name) {
return this.__attributeCache.filter(attr => attr.name === name);
} }
else { else {
return this.attributeCache; return this.__attributeCache.slice();
} }
} }
@ -176,7 +183,7 @@ class NoteShort {
* @returns {Promise<Attribute[]>} all note's labels (attributes with type label), including inherited ones * @returns {Promise<Attribute[]>} all note's labels (attributes with type label), including inherited ones
*/ */
async getLabels(name) { async getLabels(name) {
return (await this.getAttributes(name)).filter(attr => attr.type === LABEL); return await this.getAttributes(LABEL, name);
} }
/** /**
@ -184,7 +191,7 @@ class NoteShort {
* @returns {Promise<Attribute[]>} all note's label definitions, including inherited ones * @returns {Promise<Attribute[]>} all note's label definitions, including inherited ones
*/ */
async getLabelDefinitions(name) { async getLabelDefinitions(name) {
return (await this.getAttributes(name)).filter(attr => attr.type === LABEL_DEFINITION); return await this.getAttributes(LABEL_DEFINITION, name);
} }
/** /**
@ -192,7 +199,7 @@ class NoteShort {
* @returns {Promise<Attribute[]>} all note's relations (attributes with type relation), including inherited ones * @returns {Promise<Attribute[]>} all note's relations (attributes with type relation), including inherited ones
*/ */
async getRelations(name) { async getRelations(name) {
return (await this.getAttributes(name)).filter(attr => attr.type === RELATION); return await this.getAttributes(RELATION, name);
} }
/** /**
@ -200,7 +207,7 @@ class NoteShort {
* @returns {Promise<Attribute[]>} all note's relation definitions including inherited ones * @returns {Promise<Attribute[]>} all note's relation definitions including inherited ones
*/ */
async getRelationDefinitions(name) { async getRelationDefinitions(name) {
return (await this.getAttributes(name)).filter(attr => attr.type === RELATION_DEFINITION); return await this.getAttributes(RELATION_DEFINITION, name);
} }
/** /**
@ -299,8 +306,8 @@ class NoteShort {
* Clear note's attributes cache to force fresh reload for next attribute request. * Clear note's attributes cache to force fresh reload for next attribute request.
* Cache is note instance scoped. * Cache is note instance scoped.
*/ */
invalidateAttributeCache() { invalidate__attributeCache() {
this.attributeCache = null; this.__attributeCache = null;
} }
/** /**
@ -321,7 +328,7 @@ class NoteShort {
const dto = Object.assign({}, this); const dto = Object.assign({}, this);
delete dto.treeCache; delete dto.treeCache;
delete dto.archived; delete dto.archived;
delete dto.attributeCache; delete dto.__attributeCache;
return dto; return dto;
} }