All components have a well-defined lifecycle. Below is a list of methods you can implement on your view-model in order to hook into the component lifecycle:
constructor() : The view-model's constructor is called first.
created(owningView: View, myView: View) : If the view-model implements the
created callback it is invoked next. At this point in time, the view has also been created and both the view-model and the view are connected to their controller. The created callback will receive the instance of the "owningView". This is the view that the component is declared inside of. If the component itself has a view, this will be passed second.
bind(bindingContext: Object, overrideContext: Object) : Databinding is then activated on the view and view-model. If the view-model has a
bind callback, it will be invoked at this time. The "binding context" to which the component is being bound will be passed first. An "override context" will be passed second. The override context contains information used to traverse the parent hierarchy and can also be used to add any contextual properties that the component wants to add.
attached() : Next, the component is attached to the DOM (in document). If the view-model has an
attached callback, it will be invoked at this time.
detached() : If defined on your view-model - is invoked after the component has been removed from the DOM. Due to navigating away or other reasons.
unbind() : After a component is detached, it's usually unbound. If your view-model has the
unbind callback, it will be invoked during this process.
Each of these callbacks is optional. Implement whatever makes sense for your component, but don't feel obligated to implement any of them if they aren't needed for your scenario. Usually, if you implement bind you will need to implement
unbind. The same goes for
detached, but again, it isn't mandatory.