From 20b9a32209f79d9cdcd44f21b0f48395924fd352 Mon Sep 17 00:00:00 2001 From: Vu Quang Tam Date: Fri, 10 Nov 2017 19:51:29 +0700 Subject: [PATCH] Add find by attribute and value Add find object by attribute & value --- src/jsonapi-datastore.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/jsonapi-datastore.js b/src/jsonapi-datastore.js index 183708e..5cebc2f 100644 --- a/src/jsonapi-datastore.js +++ b/src/jsonapi-datastore.js @@ -114,7 +114,19 @@ class JsonApiDataStore { if (!this.graph[type] || !this.graph[type][id]) return null; return this.graph[type][id]; } - + /** + * Retrieve all models by type, attribute and value. + * @method findByAttr + * @param {string} type The type of the model. + * @return {object} The corresponding model if present, and null otherwise. + */ + findByAttr(type, attr, value) { + if (!this.graph[type]) return null; + var typeDatas = this.graph[type]; + for (var typeData in typeDatas) { + if(typeDatas[typeData][attr] == value) return typeDatas[typeData]; + } + } /** * Retrieve all models by type. * @method findAll