Skip to content

Add find by attribute and value #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: error-handling
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using Array.filter? Here you don't really retrieve all models matching attribute and value, only the first one.

for (var typeData in typeDatas) {
if(typeDatas[typeData][attr] == value) return typeDatas[typeData];
}
}
/**
* Retrieve all models by type.
* @method findAll
Expand Down