Skip to content

Commit

Permalink
default values in get
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Jan 31, 2022
1 parent 4989eaa commit d16f011
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,18 +481,18 @@ The store emits the following events:
Each object created is enriched with the following methods.


| Method | Description |
|------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| getId() | It returns a unique ID used by the store to identify the object. The ID is unique inside a single model. Be aware, `object.id` and `objet.getId()` may return different values, since store's IDs can be different from the one of the REST API. |
| set(attribute, value, hidden) | A method to set an attribute to the object. It provides some advantages compared to doing `object.attribute = value`, these are discussed in [below](#editing-objects). |
| save() | Method to save the object. You can do `store.save()` instead. |
| destroy() | Method to delete the object. You can do `store.delete()` instead. |
| get(attribute) | If you like symmetry and since you do `.set()` you would like to do also `.get()` of the attributes. It does not provide any advantage compared to accessing directly the attribute (e.g., `author.name`). |
| getRelation(model, filterFunction) | To get all the objects respecting a specific relation with this object (see [model relations](#model-relations)). |
| toJSON() | It returns a pure JSON representation of the object. |
| toString() | It returns a string representation of the object. |
| getFingerprint() | It returns a hash of the object. The hash changes at every change of the object or of any nested object. Useful to detect object changes. |
| getModel() | It returns the model of this object. Mostly useful to do `object.getModel().getType()` and obtain a string defining the type of the object. |
| Method | Description |
|------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| getId() | It returns a unique ID used by the store to identify the object. The ID is unique inside a single model. Be aware, `object.id` and `objet.getId()` may return different values, since store's IDs can be different from the one of the REST API. |
| set(attribute, value, hidden) | A method to set an attribute to the object. It provides some advantages compared to doing `object.attribute = value`, these are discussed in [below](#editing-objects). |
| save() | Method to save the object. You can do `store.save()` instead. |
| destroy() | Method to delete the object. You can do `store.delete()` instead. |
| get(attribute, defaultValue) | Method to retrieve the value of an attribute. It does not provide any advantage compared to accessing directly the attribute (e.g., `author.name`); except for hidden fields, which can be retrieved only with the `.get` method. Additionally, you can provide a default value as a second parameter in case the object doesn't have that attribute. |
| getRelation(model, filterFunction) | To get all the objects respecting a specific relation with this object (see [model relations](#model-relations)). |
| toJSON() | It returns a pure JSON representation of the object. |
| toString() | It returns a string representation of the object. |
| getFingerprint() | It returns a hash of the object. The hash changes at every change of the object or of any nested object. Useful to detect object changes. |
| getModel() | It returns the model of this object. Mostly useful to do `object.getModel().getType()` and obtain a string defining the type of the object. |

## Editing objects
The option `autoSave` can be `true`, `false`, or a number (milliseconds).
Expand Down
4 changes: 2 additions & 2 deletions src/Obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default class Obj {
return fingerprint(this.toJSON());
};

get = (attribute) => {
return this.#setHidden[attribute] || this[attribute];
get = (attribute, defaultValue) => {
return this.#setHidden[attribute] ?? this[attribute] ?? defaultValue;
};

getRelation = (type, filterFunction) => {
Expand Down

0 comments on commit d16f011

Please sign in to comment.