COMPONENT ANNOTATIONS
Overview
Since Still.js is built with vanilla JavaScript, which doesn't support annotations, it uses JavaScript comments and JSDoc to enable annotation-like capabilities. Still.js defines its own specific annotation format for this purpose.
Still.js currently supports 5 annotations, 4 of which are top-level and must appear at the top. These annotations are used for specific scenarios, allowing components to have metadata available at runtime.
Still Command Line Tool
Changes @Path from @ServicePath
If you're using Still.js version <=0.0.17 the @ServicePath
annotation
is named @Path
.
Annotation | Required complement | Description | Use context |
@Prop |
N/A | Annotates a component variables to specify that it'll be a property and not a state. |
|
@Proxy |
N/A | Define a class property which serve as a link between Parent and a child component (embeded). JSDoc @type can complement providing type hinting. |
|
@Inject |
JSDoc @type | Define a class property which serve as a link between Parent and a child component (embeded) |
|
@Controller |
JSDoc @type | Alternative to @Inject which in addition to injection adds the specified controller as the main controller of the component allowing it to be referenced as controller. in the template |
|
@Path | N/A | Complementary to the @Inject or @Controller annotation and recieves the path of the service being injected. If used it needs to come right after @Inject or @Controller annotation |
|
Examples Setup
The examples in this documentation/tutorials will be base in the bellow folder structure, Application Setup (app-setup.js
) and routes metadata (route.map.js
)
project-root-folder
|__ @still/
|__ app/
| |
| |__ base-components/
| | |__ HomeComponent.js
| | |
| |__ person/
| | |__ PersonForm.js
| | |__ PersonList.js
| | |
| |__ services/
| | |__ MainService.js
| | |__ user/
| | | |__ UserService.js
| | | |
|__ config/
| |__ app-setup.js
| |__ route.map.js
|__ ...
This is the where Application context aspects are setup. This file is in the root folder. | |
---|---|
@Prop - Defining component property variable
This example takes into consideration the above folder structure, app and routing setup.
By default, variables in a Still.js component are reactive state variables unless it's a special one like isPublic
, template
, $parent
, etc. If reactivity isn't needed, it's recommended to use the @Prop
annotation to define a non-reactive variable instead.
Annotations can be defined in the same line as the variable like in line 10 of previous example. Prop
variables can also be bound (line 18) just as State
variables, but changes to it won't reflect to binding.
Although Prop
does not reactively reflect to binding, it natually have the expected reflection in the component internal flow (e.g. line 30) thereby totally suitable to be used in any business logic.
@Prop Special case of Reactive effect
This example takes into consideration the above folder structure, app and routing setup.
Prop
variables are made not to behave reactively, however, when combined with (showIf)
directive it will act reactively against it, follow the example with animated result right after:
Animated result:
Using @Proxy for Parent to child components communication
This example takes into consideration the above folder structure, app and routing setup.
This is an approach provided in Still.js that enables parent-to-child components communication, in this case, parent needs to define a @Proxy
for every single child, this needs to be referenced in <st-element proxy="myProxyVar">
tag. It's also nice to have the @type
definition as it'll help with development experience in the intelicesnse and type hinting.
Bellow is a code sample with the animated result right after it:
Animated result
@Inject - Use Services for Global store and HTTP services
This example takes into consideration the above folder structure, app and routing setup.
It allows Service dependency injection offering a centralized way to handle data processing and bsiness logic. Services also provide Global Storage and are ideal for implementing HTTP calls.
Still.js uses a setter method at the application level (e.g., in app-setup.js
) to define the folder for services. If services are located elsewhere, the @Path
annotation can be used to speficy it in the example after this one.
Result picture:
@Controller - Sharing UI feature between components
Just like the @Inject
, the @Controller
annotation allow to inject a controller to a component where a special situation as well as a constraint takes place as follow:
-
Special case: The controller annotated with
@Controller
is marked as the main controller of the component, which can be referenced in the template for HTML event binding usingcontroller.methodName()
. -
Contraint: Only one controller can be annotate with
@Controller
, if more than one, the last one will take precedence. In contrast@Inject
can be used more than one.
Example:
The controller | |
---|---|
Injection in the component | |
---|---|
Referencing in the component template | |
---|---|
Referencing in anoter component template which didn't annotate with @Controller, or didn't inject at all | |
---|---|
Controllers are a way for components to share features whereas the Services are for Data sharing, therefore, the use case for it is for when we have a component which can have different child or sibling, but, only the main component should inject as @Controller
, other components will inject it normally or only reference in the template.
@Path - Specifying service path when @Injecting
This example takes into consideration the above folder structure, app and routing setup.
@Path
is a second level annotation that might to come hence that have to come right after @Inject. It recieves the path to the folder where the service is located. Follow the code sample with the animated result:
Animated Result: