Skip to main content

Provider class

📖 Description

The Provider class provides access to a collection in the Tripetto builder.

tip

To construct new collections, use the of function.

🗃️ Fields


🏷️ all

Retrieves all the items that are in the collection.

Type

Item[]


🏷️ 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.

info

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.

tip

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

NameTypeOptionalDescription
onCreate(item: Item) => voidYesSpecifies 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.

caution

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 the duplicate method to indicate the item is ready.

Signature

duplicate(src: Item): {
item: Item;
done: () => void;
}

Parameters

NameTypeOptionalDescription
srcItemNoSpecifies 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

NameTypeOptionalDescription
fn(item: Item) => boolean | voidNoFunction 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.

tip

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

NameTypeOptionalDescription
indexnumberYesSpecifies the index of the new item (default is 0).
onCreate(item: Item) => voidYesSpecifies 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

NameTypeOptionalDescription
idstringNoSpecifies 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

NameTypeOptionalDescription
fn(item: Item) => boolean | voidNoFunction 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

NameTypeOptionalDescription
fn(a: Item, b: Item) => booleanNoSpecifies 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.