Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 39 ยท Projects and Practice

Project: A Task Board

Build a kanban task board in Ember: columns and cards as tracked classes, moving cards, derived counts and WIP limits, an undo history, and tested board logic.

The second project is a kanban task board: columns such as To do, Doing and Done, each holding cards you can add, edit and move. It is a good reactivity workout because one action (moving a card) affects several parts of the screen at once: both columns, the counters in their headers, and an "undo" button. You will see why modeling the domain logic separately from the components makes the app easy to test and change.

Real Ember vs stand-in. Ember files are real code shown read-only. The board's rules are also given as plain JavaScript that you can run and extend here, since the site cannot run an Ember app.

Design the data first

Before components, decide what the data is:

  • A Card has an id, a title and a priority.
  • A Column has a name, an optional work-in-progress (WIP) limit and an ordered list of cards.
  • A Board owns columns and implements the rules: add, move, remove, undo.

The key design choice is that cards live inside columns (an array per column), not in one flat list with a status field. Moving a card then means removing it from one array and inserting it into another at a position. That makes ordering natural, and it makes counts a simple cards.length.

flowchart TD
  B["Board (service)"] --> C1["Column: To do"]
  B --> C2["Column: Doing (limit 3)"]
  B --> C3["Column: Done"]
  C1 --> K1["Card"]
  C1 --> K2["Card"]
  C2 --> K3["Card"]

A board service owns columns; each column owns its ordered cards.

The tracked classes

app/utils/board.js

JavaScript
import { tracked } from '@glimmer/tracking';
import { TrackedArray } from 'tracked-built-ins';

export class Card {
  @tracked title;
  @tracked priority;

  constructor(id, title, priority = 'normal') {
    this.id = id;
    this.title = title;
    this.priority = priority;
  }
}

export class Column {
  cards = new TrackedArray();

  constructor(name, limit = Infinity) {
    this.name = name;
    this.limit = limit;
  }

  get count() {
    return this.cards.length;
  }

  get isFull() {
    return this.count >= this.limit;
  }
}

Because cards is a TrackedArray, any method that inserts or removes a card updates the column and every counter that reads count. The card's own fields are @tracked, so renaming a card changes only that card's text.

The board service

app/services/board.js

JavaScript
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { Card, Column } from 'tasks/utils/board';

export default class BoardService extends Service {
  columns = [new Column('To do'), new Column('Doing', 3), new Column('Done')];
  @tracked history = [];
  #nextId = 1;

  get totalCards() {
    return this.columns.reduce((sum, column) => sum + column.count, 0);
  }

  get canUndo() {
    return this.history.length > 0;
  }

  column(name) {
    return this.columns.find((c) => c.name === name);
  }

  add(columnName, title) {
    const card = new Card(this.#nextId++, title);
    this.column(columnName).cards.push(card);
    return card;
  }

  move(cardId, toName, toIndex = Infinity) {
    const from = this.columns.find((c) => c.cards.some((card) => card.id === cardId));
    const to = this.column(toName);
    if (!from || !to) throw new Error('Unknown card or column');
    if (from !== to && to.isFull) return false; // WIP limit

    const fromIndex = from.cards.findIndex((card) => card.id === cardId);
    const [card] = from.cards.splice(fromIndex, 1);
    to.cards.splice(Math.min(toIndex, to.cards.length), 0, card);
    this.history = [...this.history, { cardId, fromName: from.name, fromIndex }];
    return true;
  }

  undo() {
    const last = this.history.at(-1);
    if (!last) return;
    this.history = this.history.slice(0, -1);
    const current = this.columns.find((c) => c.cards.some((card) => card.id === last.cardId));
    const index = current.cards.findIndex((card) => card.id === last.cardId);
    const [card] = current.cards.splice(index, 1);
    this.column(last.fromName).cards.splice(last.fromIndex, 0, card);
  }
}

Three rules show up as code: a WIP limit (a full column refuses new cards), insertion at an index, and undo by remembering where each moved card came from.

The components

The components are thin: they show service state and call service methods.

app/components/board-column.gjs

JavaScript
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { action } from '@ember/object';
import { on } from '@ember/modifier';
import { fn } from '@ember/helper';

export default class BoardColumn extends Component {
  @service board;

  @action moveTo(card, target) {
    this.board.move(card.id, target);
  }

  <template>
    <section data-test-column={{@column.name}}>
      <h2>{{@column.name}} <span data-test-count>({{@column.count}})</span></h2>
      {{#each @column.cards as |card|}}
        <article data-test-card>
          {{card.title}}
          {{#each @targets as |target|}}
            <button type="button" {{on "click" (fn this.moveTo card target)}}>
              To {{target}}
            </button>
          {{/each}}
        </article>
      {{/each}}
    </section>
  </template>
}

Notice there is no drag-and-drop here. Buttons are easier to build, fully keyboard accessible and fully testable with click. You can add drag and drop later as an extra way to call the same board.move.

Run the rules

Here are the same rules in plain JavaScript. Run it, then try breaking the WIP limit or undoing several times.

JS The board logic, runnable

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The third move prints false: the WIP limit of two stopped it. The undo restored card a to its old position in Doing.

Derived values for the header

A board usually shows progress. Because the data is tracked, these are simple getters:

JS Progress numbers derived from the columns

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In Ember these would be get totalCards(), get doneCount() and get percentDone() on the service. Nothing has to say "recalculate when a card moves": the getters read tracked data, so they update themselves (Derived State).

Extensions

  1. Edit a card inline with a tracked editing flag on the card.
  2. Priorities with colors, and a filter that shows only high-priority cards (a derived visibleCards getter per column).
  3. Persistence: save the columns as JSON as in the notes app.
  4. Drag and drop with the platform's drag events or an addon, calling board.move.
  5. Multiple boards: a boards route that loads one board per URL.
  6. Tests: unit-test move (limits, order, undo) and acceptance-test moving a card with buttons.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do cards live inside each column's array instead of a flat list with a status field?

    Ordered arrays per column make insertion position and counts straightforward. A flat list would need extra position fields.
  2. 2. What does the board do when a column has a WIP limit and is full?

    The rule is enforced in one place, the service method, so every caller (buttons, drag and drop, tests) obeys it.
  3. 3. How does undo know where to put the card back?

    A history entry stores exactly the information needed to reverse the move.
  4. 4. Why keep the board rules in a service or plain classes rather than inside components?

    Domain logic separated from rendering can be unit-tested directly and reused by several UIs.

Summary

  • Model the domain first: board, columns, cards, with cards stored in their column.
  • Use tracked classes for editable fields and TrackedArray for ordered lists.
  • Put rules (limits, ordering, undo) in the service; keep components as thin views.
  • Derive header numbers with getters; never store totals separately.
  • Offer buttons first; add drag and drop as a second way to call the same method.

Next: Project: Building a Test Suite