Side-by-side comparison
Bootstrapping
The hosting page
The first entry point of an Aurelia application is the main HTML page-loading and hosting.
aurelia-app
attribute helps us to introduce our entry point, themain.ts
file, which includes the configurations of the project.
The main module
All the initial settings for starting and working with an Aurelia project are done in this file.
What does PLATFORM.moduleName
do?
Whenever you reference a module by string, you need to usePLATFORM.moduleName("moduleName")
to wrap the bare string. PLATFORM.moduleName
is designed to teachWebpack
about Aurelia's dynamic loading behavior.
What is a globalResources
?
When you create a view in Aurelia, it is completely encapsulated so you mustrequire
components into an Aurelia view. However, certain components are used so frequently across views that it can become very tedious to import them over and over again. To solve this problem, Aurelia lets you explicitly declare certain "view resources" as global.
What is a feature
?
Sometimes you have a whole group of components or related functionality that collectively form a "feature". This "feature" may even be owned by a particular set of developers on your team. You want these developers to be able to manage the configuration and resources of their own feature, without interfering with the other parts of the app. For this scenario, Aurelia provides the "feature".
What is a plugin
?
Similar to features, you can install 3rd party plugins. The main difference is that a "feature" is provided internally by your application, while a plugin is installed from a 3rd party source through your package manager.
What does setRoot()
do?
Instantiates the root component and adds it to the DOM.
Components
The Root Component
The root of any Aurelia application is a single
component, which contains everything within the application, actually, the root component.
To import any style, component or etc, you should use
require
.Wrapping the whole HTML content via
template
isnecessary
.
The Component Life-cycle
Every component instance has a life-cycle that you can tap into. This makes it easy for you to perform various actions at particular times
Name | Aurelia 1 | Asyncable | Description |
---|---|---|---|
constructor | constructor | ✗ | |
define | ✗ | ✗ | |
hydrating | ✗ | ✗ | |
hydrated | ✗ | ✗ | |
created | created | ✗ | |
binding | bind | ✓ | |
bound | ✗ | ✓ | |
attaching | ✗ | ✓ | |
attached | attached | ✓ | |
detaching | ✗ | ✓ | |
unbinding | unbind | ✓ | |
dispose | ✗ | ✗ |
Aurelia 1 has a restriction and the community made an afterAttached plugin that is called after all child components are attached, and after all two-way bindings have completed. Theattached
life-cycle in version 2 covers this scenario.
Which life-cycle hooks are most used?
Such cases can be summarized.
Name | When using it |
---|---|
binding | Fetch data (working with API services & Ajax calls), initialize data/subscriptions. |
bound | Any work that relies on fromView/twoWay binding data coming from children, Defining router hooks. |
attached | Use anything (like third-party libraries) that touches the DOM. |
unbinding | Cleanup data/subscriptions, maybe persist some data for the next activation. |
dispose | One way cleanup all the references/resources. This is invoked only once and is irreversible |
Dependency injection
A dependency injection container is a tool that can simplify the process of decomposing such a system. Oftentimes, when developers go through the work of destructuring a system, they introduce a new complexity of "re-assembling" the smaller parts again at runtime. This is what a dependency injection container can do for you, using simple declarative hints.
Registering services
Aurelia 1 | Aurelia 2 | Description |
---|---|---|
container.createChild() | DI.createContainer() | - |
container.registerSingleton(key: any, fn?: Function) | Registration.singleton(key: any, value: Function): IRegistration | - |
container.registerTransient(key: any, fn?: Function) | Registration.transient(key: any, value: Function): IRegistration | - |
container.registerInstance(key: any, instance?: any) | Registration.transient(key: any, value: any): IRegistration | - |
container.registerHandler(key, handler) | Registration.callback(key: any, value: ResolveCallback): IRegistration | - |
container.registerResolver(key: any, resolver: Resolver) | container.registerResolver(key: any, resolver: IResolver) | - |
container.autoRegister(key: any, fn?: Function | ✗ | - |
✗ | Registration.alias(originalKey: any, aliasKey: any): IRegistration | - |
Resolving services
Aurelia 1 | Aurelia 2 | Description |
---|---|---|
container.get(MyService) | container.get(MyService) | - |
✗ | container.getAll(MyService) | - |
Registration strategies
Name | Aurelia 1 & 2 | Description |
---|---|---|
@singleton | ✓ | - |
@transient | ✓ | - |
Resolvers
Aurelia 1 | Aurelia 2 | Description |
---|---|---|
@inject(MyService) | @inject(MyService) | - |
@autoinject() | ✗ | |
@inject(Lazy.of(MyService)) | @inject(lazy(MyService)) | - |
@inject(All.of(MyService)) | @inject(all(MyService)) | - |
@inject(Optional.of(MyService)) | @inject(optional(MyService)) | - |
@inject(Parent.of(MyService)) | ✗ | - |
@inject(Factory.of(MyService)) | @inject(factory(MyService)) | - |
@inject(NewInstance.of(MyService)) | @inject(newInstanceForScope(MyService)) | - |
✗ | @inject(newInstanceOf(MyService)) | - |
Logging
Writing debug output while developing is great. This is how you can do this with Aurelia.
Write an appender.
In the main(.js|.ts)
Router
Routing Life-cycle
Name | Description |
---|---|
canActivate | if the component can be activated. |
activate | when the component gets activated. |
canDeactivate | if the component can be deactivated. |
deactivate | when the component gets deactivated. |
Binding
String Interpolation
Name | Aurelia 1 & 2 | Description |
---|---|---|
${ } | ✓ |
Binding HTML and SVG Attributes
Name | Aurelia 1 & 2 | Description |
---|---|---|
one-way | ✓ | |
to-view | ✓ | |
from-view | ✓ | |
two-way | ✓ | |
one-time | ✓ | |
bind | ✓ |
References
| Name | Aurelia 1 | Aurelia 2 | Description | | ---- | - | - | | ref | ✓ | ✓ | | | view-model.ref | ✓ | ✓ | deprecated in v2 | | component.ref | ✗ | ✓ | Not in v1 |
Passing Function References
Name | Aurelia 1 & 2 | Description |
---|---|---|
call | ✓ |
DOM Events
Name | Aurelia 1 & 2 | Description |
---|---|---|
trigger | ✓ | |
delegate | ✓ | |
capture | ✓ |
{% hint style="info" } In v2, if an expression return a function, that function will be use as the handler for the event. V1 only evaluates the expression. {% endhint }
Contextual Properties
General
Name | Aurelia 1 & 2 | Description |
---|---|---|
$this | ✓ | The view-model that your binding expressions are being evaluated against. |
Event
Name | Aurelia 1 & 2 | Description |
---|---|---|
$event | ✓ | The DOM Event in |
Repeater
Name | Aurelia 1 | Aurelia 2 | Description |
---|---|---|---|
$parent | ✓ | ✓ | |
$parent.$parent.$parent.name | ✓ | ✓ | |
$index | ✓ | ✓ | |
$first | ✓ | ✓ | |
$last | ✓ | ✓ | |
$even | ✓ | ✓ | |
$odd | ✓ | ✓ | |
$length | ✗ | ✓ |
@computedFrom
@computedFrom
tells the binding system which expressions to observe. When those expressions change, the binding system will re-evaluate the property (execute the getter).
Aurelia 1 | Aurelia 2 |
---|---|
✓ | ✗ |
In Aurelia 2, The framework automatically computes observation without the need for any configuration or decorator.
Observation in template
type | example | type | Aurelia 1 | Aurelia 2 |
---|---|---|---|---|
property |
| syntax: | ✓ | ✓ |
observation: | ✓ | ✓ | ||
member |
| syntax: | ✓ | ✓ |
observation: | ✓ | ✓ | ||
value conveter |
| syntax: | ✓ | ✓ |
observation: | ✓ | ✓ | ||
binding behavior |
| syntax: | ✓ | ✓ |
observation: | ✗ | ✗ | ||
function call |
| syntax: | ✓ | ✓ |
observation: | ✓ | ✓ | ||
array methods |
| syntax: | ✓ | ✓ |
observation (on array): | ✗ | ✓ | ||
lambda |
| syntax: | ✗ | ✓ |
observation: | ✗ | ✓ |
@attributePattern
This feature is totally new for Aurelia 2.
Last updated