Skip to content

Commit

Permalink
documented setConstant
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Jan 31, 2022
1 parent 427a27d commit cde4ae7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ If now a book is inserted/deleted/edited:
* if the book has `price < 20`, `drawBooksCallback` will be called again with the new dataset;
* if the book has `price > 20`, `drawBooksCallback` will NOT be called again (because the new book doesn't impact our selection).

> Warning: if you edit the objects inside your callback (e.g., you do `.set()`), you will trigger the subscription's callback again in an infinite loop! If you want to set an attribute of an object inside your callback, before drawing it, use `setConstant()`.
You can terminate the subscription with `store.unsubscribe()`:

```js
Expand Down Expand Up @@ -481,18 +483,20 @@ 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, 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. |
| 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). The third parameter is optional, and when set to true will set the attribute as hidden (see [hiddenFields](#models-creation)). |
| setConstant(attribute, value) | A method to set an unmodifiable hidden attribute on the object. Setting the attribute as a constant will not propagate an update. |
| 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 and constants, 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)). |
| save() | Method to save the object. You can do `store.save()` instead. |
| destroy() | Method to delete the object. You can do `store.delete()` instead. |

| 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: 4 additions & 0 deletions src/Obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export default class Obj {
return this.getModel().getStore().update([this]);
};

setConstant = (attribute, value) => {
this.#setHidden[attribute] = this.#setHidden[attribute] ?? value;
};

save = () => {
return this.getModel().getStore().save([this]);
};
Expand Down

0 comments on commit cde4ae7

Please sign in to comment.