Ember Octane vs Ember Polaris Comparison

Reactivity

Declare state

Ember Octane

name.hbs

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

Ember Polaris (preview)

name.gjs

import Component from "@glimmer/component";

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

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

Update state

Ember Octane

name.hbs

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

Ember Polaris (preview)

name.gjs

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>
}

Computed state

Ember Octane

double-count.hbs

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

Ember Polaris (preview)

double-count.gjs

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>
}

Templating

Minimal template

Ember Octane

hello-world.hbs

<h1>Hello world</h1>

Ember Polaris (preview)

hello-world.gjs

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

Styling

Ember Octane

css-style.css

/* using: https://github.com/salsify/ember-css-modules */
.title {
  color: red;
}

Ember Polaris (preview)

css-style.gjs

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

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

Loop

Ember Octane

colors.hbs

<ul>
  {{#each (array "red" "green" "blue") as |color|}}
    <li>{{color}}</li>
  {{/each}}
</ul>

Ember Polaris (preview)

colors.gjs

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

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

Event click

Ember Octane

counter.hbs

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

Ember Polaris (preview)

counter.gjs

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

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>
}

Dom ref

Ember Octane

input-focused.hbs

<input {{this.autofocus}} />

Ember Polaris (preview)

input-focused.gjs

import { modifier } from "ember-modifier";

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

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

Conditional

Ember Octane

traffic-light.hbs

<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>

Ember Polaris (preview)

traffic-light.gjs

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { on } from "@ember/modifier";
import { eq } from 'ember-truth-helpers';

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>
}

Lifecycle

On mount

Ember Octane

page-title.hbs

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

Ember Polaris (preview)

page-title.gjs

const pageTitle = () => document.title;

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

On unmount

Ember Octane

time.hbs

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

Ember Polaris (preview)

time.gjs

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>
}

Component composition

Props

Ember Octane

app.hbs

<UserProfile
  @name="John"
  @age={{20}}
  @favouriteColors={{array "green" "blue" "red"}}
  @isAvailable={{true}}
/>

Ember Polaris (preview)

app.gjs

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

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

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

Emit to parent

Ember Octane

app.hbs

<p>Are you happy?</p>
<AnswerButton @onYes={{this.handleYes}} @onNo={{this.handleNo}} />
<p style="font-size: 50px;">{{if this.isHappy "😀" "😥"}}</p>

Ember Polaris (preview)

answer-button.gjs

import { on } from "@ember/modifier";

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

Slot

Ember Octane

app.hbs

<FunnyButton> Click me! </FunnyButton>

Ember Polaris (preview)

app.gjs

import FunnyButton from "./funny-button";

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

Slot fallback

Ember Octane

app.hbs

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

Ember Polaris (preview)

app.gjs

import FunnyButton from "./funny-button";

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

Context

Ember Octane

app.hbs

<UserProfile />

Ember Polaris (preview)

app.gjs

import UserProfile from "./user-profile";

<template>
  <UserProfile />
</template>;

Form input

Input text

Ember Octane

input-hello.hbs

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

Ember Polaris (preview)

input-hello.gjs

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { on } from '@ember/modifier';

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>
}

Checkbox

Ember Octane

is-available.hbs

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

Ember Polaris (preview)

is-available.gjs

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { on } from '@ember/modifier';

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>
}

Radio

Ember Octane

pick-pill.hbs

<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>

Ember Polaris (preview)

pick-pill.gjs

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { on } from '@ember/modifier';
import { eq } from 'ember-truth-helpers';

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>
}

Select

Ember Octane

color-select.hbs

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

Ember Polaris (preview)

color-select.gjs

import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { on } from '@ember/modifier';

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>
}

Webapp features

Render app

Ember Octane

index.html

<h1>Hello world</h1>

Ember Polaris (preview)

index.html

<h1>Hello world</h1>

Fetch data

Ember Octane

app.hbs

{{#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.users as |user|}}
        <li>
          <img src={{user.picture.thumbnail}} alt="user" />
          <p>{{user.name.first}} {{user.name.last}}</p>
        </li>
      {{/each}}
    </ul>

  {{/if}}
{{/let}}

Ember Polaris (preview)

app.gjs

// Fetch data example for Ember Polaris
// This is a placeholder for the actual implementation






Decouvrez plus d’Offres de la plateform ItGalaxy.io :

Découvrez notre gamme complète de services et formations pour accélérer votre carrière.

1. Nous contactez

  • Description: Besoin de Formation et des Solutions cloud complètes pour vos applications
  • Links:

2. Infra as a Service

  • Description: Infrastructure cloud évolutive et sécurisée
  • Links:

3. Projets Développeurs


4. Développeurs


5. Formations Complètes


6. Marketplace

7. Blogs


This website is powered by ItGalaxy.io