Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular to Aurelia Transition - Some basic questions

We are thinking about using Aurelia for a new app. I come from an Angular 1 background (with some exposure to Angular 2). Aurelia seems quite nice and I really like how they have taken on the responsibility of maintaining developer workflow. However I have some questions which I cannot seem to find the answers to:

1) There are two general ways (as I understand) that one can include a web component in a page. These are <compose> and write a custom element. My question is, coming from Angular there is a big emphasis on scopes (i.e. what is in scope at a specific point in the DOM). I am wondering what is in "scope" (i.e. available to binding expression) withing compose and custom elements. I mean, is a parent view-model available within a child template? If so, do child view-model properties hide/shadow the parent view-model properties?

2) In Angular 2, there are guidelines with respect to how data is passed in and out of components. We are not supposed to change non-primitives that are bound to a component (because that forces Angular 2's change detection to always go into that component branch to check all properties).

Are there any guidelines/info to passing data in and out of components in Aurelia? I mean from the reading that I have done it sounds like I would just use value.bind to bind to all the @bindable properties. Is that correct? Are these two-way bound by default or do I have to explicitly declare value.two-way? Is there anything wrong with changing the properties of a two-way bound object?

Thanks in advance

like image 352
pQuestions123 Avatar asked Mar 08 '16 22:03

pQuestions123


People also ask

Is Aurelia similar to Angular?

The most obvious difference to non-developers between the two frameworks is that Angular has a more extensive user base and community, primarily due to Google's backing. However, Aurelia is more stable than Angular, so Aurelia would be preferable in high-end applications where quality and security have great value.

What is Aurelia framework?

Aurelia is an open-source UI JavaScript framework designed to create single page applications (SPAs) that doesn't behave like a framework. It's been built from the ground up using modern tooling and ECMAScript 2016 with full support for TypeScript.

Is Aurelia a good framework?

It offers great rendering speed and memory efficiency: Aurelia implements batch rendering and observable object pooling, which allows it to render faster than any other framework. Aurelia also utilizes less memory and causes less GC churn than other frameworks.


1 Answers

Great questions- here's some info:

1) There are two general ways (as I understand) that one can include a web component in a page. These are <compose> and write a custom element. My question is, coming from Angular there is a big emphasis on scopes (i.e. what is in scope at a specific point in the DOM). I am wondering what is in "scope" (i.e. available to binding expression) within compose and custom elements. I mean, is a parent view-model available within a child template? If so, do child view-model properties hide/shadow the parent view-model properties?

Consider the following markup:

app.html

<template>
  <h1>${message}</h1>

  <date-picker value.bind="startDate"></date-picker>

  <compose view="footer.html"></compose>
<template>

<compose> preserves access to the outer scope. When the template contained in footer.html is data-bound, it will have access to whatever app.html is bound to- for example, it could access the message property.

A custom element's template cannot access the outer scope. Custom elements are intended to be encapsulated and portable. For this reason they are not allowed to access the outer scope, preventing developers from creating custom elements that expect to be used in specific binding contexts. The primary way to get data in/out of a custom element is through @bindable properties (similar to dependency-properties in XAML).

2) In Angular 2, there are guidelines with respect to how data is passed in and out of components. We are not supposed to change non-primitives that are bound to a component (because that forces Angular 2's change detection to always go into that component branch to check all properties).

Are there any guidelines/info to passing data in and out of components in Aurelia? I mean from the reading that I have done it sounds like I would just use value.bind to bind to all the @bindable properties. Is that correct?

correct

Are these two-way bound by default or do I have to explicitly declare value.two-way? Is there anything wrong with changing the properties of a two-way bound object?

Aurelia automatically chooses the correct default binding mode for attributes of built-in elements like inputs or selects. You need to specify the defaults for custom elements if you want something other than one-way. Here's how to do that:

import {bindingMode} from 'aurelia-framework'; // or 'aurelia-binding';

export class DatePicker {
  @bindable({ defaultBindingMode: bindingMode.twoWay }) value;  // <----
  @bindable min = new Date(1900, 0, 1);
  @bindable max = new Date(2250, 11, 31);
  ...
}
like image 185
Jeremy Danyow Avatar answered Oct 05 '22 12:10

Jeremy Danyow