Provider class
📖 Description
The Provider
class provides access to a collection in the Tripetto builder.
To construct new collections, use the of
function.
🗃️ Fields
🏷️ all
Retrieves all the items that are in the collection.
Type
🏷️ block
Retrieves the block for the collection (if available).
Type
NodeBlock
| ConditionBlock
| undefined
🏷️ count
Retrieves the number of items in the collection.
Type
number
🏷️ firstItem
Retrieves the first item in the collection.
Type
Item
| undefined
🏷️ lastItem
Retrieves the last item in the collection.
Type
Item
| undefined
🏷️ ref
Retrieves a reference to the parent of the collection.
The reference is set using the reference
argument of the of
function.
Type
NodeBlock
| ConditionBlock
| Item
▶️ Methods
🔧 append
Appends a new item to the end of the collection.
The optional onCreate
parameter is only used under certain conditions. In most cases, you can omit it and call append()
.
Signature
append(onCreate?: (item: Item) => void): Item
Parameters
Name | Type | Optional | Description |
---|---|---|---|
onCreate | (item: Item ) => void | Yes | Specifies a function that can be used to set item properties before @created decorated methods are invoked. |
Return value
Returns a reference to the new Item
instance.
🔧 duplicate
Duplicates an item in the collection.
Duplicating an item in a collection requires two steps:
- Step 1: Call this
duplicate
method; - Step 2: Set the properties of the new duplicated item;
- Step 3: Call the
done
function returned by theduplicate
method to indicate the item is ready.
Signature
duplicate(src: Item): {
item: Item;
done: () => void;
}
Parameters
Name | Type | Optional | Description |
---|---|---|---|
src | Item | No | Specifies the source item to duplicate. |
Return value
Returns an object with a reference to the duplicate and a function that should be called when the duplicated item is ready. This allows you to configure the duplicated item.
🔧 each
Iterates through all items in the collection.
Signature
each(fn: (item: Item) => boolean | void): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
fn | (item: Item ) => boolean | void | No | Function that is called for each item in the collection. If you want to stop the iteration, you can return true from this function to do so. |
Return value
Returns true
if the iteration was stopped (break).
🔧 insert
Inserts a new item to the collection.
The optional onCreate
parameter is only used under certain conditions. In most cases, you can omit it and call insert()
.
Signature
insert(index?: number, onCreate?: (item: Item) => void): Item
Parameters
Name | Type | Optional | Description |
---|---|---|---|
index | number | Yes | Specifies the index of the new item (default is 0 ). |
onCreate | (item: Item ) => void | Yes | Specifies a function that can be used to set item properties before @created decorated methods are invoked. |
Return value
Returns a reference to the new Item
instance.
🔧 itemByIdentifier
Retrieves the item with the specified identifier.
Signature
itemByIdentifier(id: string): Item | undefined
Parameters
Name | Type | Optional | Description |
---|---|---|---|
id | string | No | Specifies the identifier. |
Return value
Returns the Item
or undefined
if the item is not found.
🔧 refresh
Refreshes all items in the collection.
Signature
refresh(): void
🔧 reverseEach
Iterates through all items in the collection in reverse direction (starting with the last item).
Signature
reverseEach(fn: (item: Item) => boolean | void): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
fn | (item: Item ) => boolean | void | No | Function that is called for each item in the collection. If you want to stop the iteration, you can return true from this function to do so. |
Return value
Returns true
if the iteration was stopped (break).
🔧 sort
Sorts the items using the supplied compare function.
Signature
sort(fn: (a: Item, b: Item) => number): boolean
Parameters
Name | Type | Optional | Description |
---|---|---|---|
fn | (a: Item , b: Item ) => boolean | No | Specifies the compare function. The compare function should return a number according to the following rules: - a is less than b return -1;- a is greater than b return 1;- a is equal to b return 0. |
Return value
Returns true
if the sort invoked an item position change.