Reactivity

Declare state

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";

export default class NameComponent extends Component {
  name = "John";

  <template>
    <h1>Hello {{this.name}}</h1>
  </template>
}

logo of Aurelia 2 Aurelia 2

<h1>Hello ${name}</h1>

Update state

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class CounterComponent extends Component {
  @tracked name = "John";

  constructor(owner, args) {
    super(owner, args);

    this.name = "Jane";
  }

  <template>
    <h1>Hello {{this.name}}</h1>
  </template>
}

logo of Aurelia 2 Aurelia 2

<h1>Hello ${name}</h1>

Computed state

logo of Ember Polaris Ember Polaris

import Component, { tracked } from "@glimmer/component";

export default class DoubleCount extends Component {
  @tracked count = 10;

  get doubleCount() {
    return this.count * 2;
  }

  <template>
    <div>{{this.doubleCount}}</div>
  </template>
}

logo of Aurelia 2 Aurelia 2

<div>${doubleCount}</div>

Templating

Minimal template

logo of Ember Polaris Ember Polaris

<template>
  <h1>Hello world</h1>
</template>

logo of Aurelia 2 Aurelia 2

<h1>Hello world</h1>

Styling

logo of Ember Polaris Ember Polaris

<template>
  <h1 class="title">I am red</h1>
  <button style="font-size: 10rem;">I am a button</button>

  <style>
    .title {
      color: red;
    }
  </style>
</template>

logo of Aurelia 2 Aurelia 2

.title {
  color: red;
}

Loop

logo of Ember Polaris Ember Polaris

const colors = ["red", "green", "blue"];

<template>
  <ul>
    {{#each colors as |color|}}
      <li>{{color}}</li>
    {{/each}}
  </ul>
</template>

logo of Aurelia 2 Aurelia 2

<ul>
  <li repeat.for="color of colors">${color}</li>
</ul>

Event click

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class Counter extends Component {
  @tracked count = 0;

  incrementCount = () => this.count++;

  <template>
    <p>Counter: {{this.count}}</p>
    <button {{on "click" this.incrementCount}}>+1</button>
  </template>
}

logo of Aurelia 2 Aurelia 2

<p>Counter: ${count}</p>
<button click.trigger="incrementCount">+1</button>

Dom ref

logo of Ember Polaris Ember Polaris

import { modifier } from "ember-modifier";

const autofocus = modifier((element) => element.focus());

<template>
  <input {{autofocus}} />
</template>

logo of Aurelia 2 Aurelia 2

<input ref="inputElement" />

Conditional

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

const TRAFFIC_LIGHTS = ["red", "orange", "green"];

export default class TrafficLight extends Component {
  @tracked lightIndex = 0;

  get light() {
    return TRAFFIC_LIGHTS[this.lightIndex];
  }

  nextLight = () => {
    this.lightIndex = (this.lightIndex + 1) % TRAFFIC_LIGHTS.length;
  };

  <template>
    <button {{on "click" this.nextLight}}>Next light</button>
    <p>Light is: {{this.light}}</p>
    <p>
      You must
      {{#if (eq this.light "red")}}
        STOP
      {{else if (eq this.light "orange")}}
        SLOW DOWN
      {{else if (eq this.light "green")}}
        GO
      {{/if}}
    </p>
  </template>
}

logo of Aurelia 2 Aurelia 2

<button click.trigger="nextLight()">Next light</button>
<p>Light is: ${light}</p>
<p switch.bind="light">
  You must
  <span case="red">STOP</span>
  <span case="orange">SLOW DOWN</span>
  <span case="green">GO</span>
</p>

Lifecycle

On mount

logo of Ember Polaris Ember Polaris

const pageTitle = () => document.title;

<template>
  <p>Page title is: {{(pageTitle)}}</p>
</template>

logo of Aurelia 2 Aurelia 2

<p>Page title is: ${pageTitle}</p>

On unmount

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { registerDestructor } from "@ember/destroyable";

export default class Time extends Component {
  @tracked time = new Date().toLocaleTimeString();

  constructor(owner, args) {
    super(owner, args);

    let timer = setInterval(() => {
      this.time = new Date().toLocaleTimeString();
    }, 1000);

    registerDestructor(this, () => clearInterval(timer));
  }

  <template>
    <p>Current time: {{this.time}}</p>
  </template>
}

logo of Aurelia 2 Aurelia 2

<p>Current time: ${time}</p>

Component composition

Props

logo of Ember Polaris Ember Polaris

import UserProfile from "./user-profile.gjs";

const favoriteColors = ["green", "blue", "red"];

<template>
  <UserProfile
    @name="John"
    @age={{20}}
    @favouriteColors={{favoriteColors}}
    @isAvailable={{true}}
  />
</template>

logo of Aurelia 2 Aurelia 2

<user-profile
  name.bind
  age.bind
  favourite-colors.bind="colors"
  is-available.bind="available"
></user-profile>

Emit to parent

logo of Ember Polaris Ember Polaris

<template>
  <button {{on "click" @onYes}}> YES </button>
  <button {{on "click" @onNo}}> NO </button>
</template>

logo of Aurelia 2 Aurelia 2

<p>Can I come ?</p>
<answer-button action-handler.bind="handleAnswer"></answer-button>
<p style="font-size: 50px">${isHappy ? "😀" : "😥"}</p>

Slot

logo of Ember Polaris Ember Polaris

import FunnyButton from "./funny-button";

<template>
  <FunnyButton>Click me!</FunnyButton>
</template>

logo of Aurelia 2 Aurelia 2

<funny-button>Click me !</funny-button>

Slot fallback

logo of Ember Polaris Ember Polaris

import FunnyButton from "./funny-button";

<template>
  <FunnyButton />
  <FunnyButton>I got content!</FunnyButton>
</template>

logo of Aurelia 2 Aurelia 2

<funny-button></funny-button>
<funny-button>Click me !</funny-button>

Context

logo of Ember Polaris Ember Polaris

import UserProfile from "./user-profile";

<template>
    <UserProfile />
</template>

logo of Aurelia 2 Aurelia 2

<h1>Welcome back, {{ user.username }}</h1>
<user-profile />

Form input

Input text

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class InputHello extends Component {
  @tracked text = "Hello World";

  handleInput = (event) => (this.text = event.target.value);

  <template>
    <p>{{this.text}}</p>
    <input value={{this.text}} {{on "input" this.handleInput}} />
  </template>
}

logo of Aurelia 2 Aurelia 2

<p>${text}</p>
<input value.bind />

Checkbox

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class InputHello extends Component {
  @tracked isAvailable = false;

  handleChange = (event) => (this.isAvailable = event.target.checked);

  <template>
    <input
      id="is-available"
      type="checkbox"
      checked={{this.isAvailable}}
      {{on "change" this.handleChange}}
    />
    <label for="is-available">Is available</label>
  </template>
}

logo of Aurelia 2 Aurelia 2

<input id="is-available" type="checkbox" checked.bind="isAvailable" />
<label for="is-available">Is available</label>: ${isAvailable}

Radio

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class PickPill extends Component {
  @tracked picked = "red";

  handleChange = (event) => (this.picked = event.target.value);

  <template>
    <div>Picked: {{this.picked}}</div>

    <input
      id="blue-pill"
      type="radio"
      value="blue"
      checked={{eq this.picked "blue"}}
      {{on "change" this.handleChange}}
    />
    <label htmlFor="blue-pill">Blue pill</label>

    <input
      id="red-pill"
      type="radio"
      value="red"
      checked={{eq this.picked "red"}}
      {{on "change" this.handleChange}}
    />
    <label htmlFor="red-pill">Red pill</label>
  </template>
}

logo of Aurelia 2 Aurelia 2

<div>Picked: ${picked}</div>

<input id="blue-pill" checked.bind="picked" type="radio" value="blue" />
<label for="blue-pill">Blue pill</label>

<input id="red-pill" checked.bind="picked" type="radio" value="red" />
<label for="red-pill">Red pill</label>

Select

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class ColorSelect extends Component {
  @tracked selectedColorId = 2;

  select = (event) => (this.selectedColorId = event.target.value);

  isSelected = (colorId) => this.selectedColorId === colorId;

  colors = [
    { id: 1, text: "red" },
    { id: 2, text: "blue" },
    { id: 3, text: "green" },
    { id: 4, text: "gray", isDisabled: true },
  ];

  <template>
    <select {{on "change" this.select}}>
      {{#each this.colors as |color|}}
        <option
          value={{color.id}}
          disabled={{color.isDisabled}}
          selected={{this.isSelected color.id}}
        >
          {{color.text}}
        </option>
      {{/each}}
    </select>
  </template>
}

logo of Aurelia 2 Aurelia 2

<select value.bind="selectedColorId">
  <option value="">Select A Color</option>
  <option
    repeat.for="color of colors"
    value.bind="color.id"
    disabled.bind="color.isDisabled"
  >
    ${color.text}
  </option>
</select>

Webapp features

Render app

logo of Ember Polaris Ember Polaris

<!doctype html>
<html>
  <body>
    <script type="module" src="./index.js"></script>
  </body>
</html>

logo of Aurelia 2 Aurelia 2

<!doctype html>
<html>
  <head>
    <script type="module" src="./main.ts"></script>
  </head>

  <body>
    <app></app>
  </body>
</html>

Fetch data

logo of Ember Polaris Ember Polaris

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

class State {
  @tracked isLoading = false;
  @tracked error = null;
  @tracked data = null;
}

function fetchUsers() {
  let state = new State();
  
  async function fetchData() {
    try {
      let response = await fetch("https://randomuser.me/api/?results=3");
      let { results: users } = await response.json();
      state.data = users;
      state.error = null;
    } catch (err) {
      state.data = null;
      state.error = err;
    }
    state.isLoading = false;
  }
  
  fetchData();
  return state;
}

export default class App extends Component {
  <template>
    {{#let (this.fetchUsers) as |request|}}
      {{#if request.isLoading}}
        <p>Fetching users...</p>
      {{else if request.error}}
        <p>An error occurred while fetching users</p>
      {{else}}
        <ul>
          {{#each request.data as |user|}}
            <li>
              <img src={{user.picture.thumbnail}} alt="user" />
              <p>{{user.name.first}} {{user.name.last}}</p>
            </li>
          {{/each}}
        </ul>
      {{/if}}
    {{/let}}
  </template>
}

logo of Aurelia 2 Aurelia 2

<template promise.bind="useFetchUsers.fetchData()">
  <p pending>Fetching users...</p>
  <p catch>An error ocurred while fetching users</p>
  <ul then.from-view="users">
    <li repeat.for="user of users">
      <img src.bind="user.picture.thumbnail" alt="user" />
      <p>${ user.name.first } ${ user.name.last }</p>
    </li>
  </ul>
</template>