Goal: As a user, I want to see the list of all games with a detailed template
Keywords: template loop, custom attribute, nested component
- Create a new custom element
x-games
- Create
games.html
andgames.dart
file and copy theGAMES_TEMPLATE
html blocks from the templates into its body
We are going to work with theGAMES_TEMPLATE_DETAILED
in this user story so comment or remove theGAMES_TEMPLATE_COMPACT
- Import and use it in your
index.html
instead ofx-game
and check the result
- One game is not enough for a Game Store, create a service to retrieve games
-
Create a file
services.dart
with the classInMemoryGameStoreService
:library game_store.service; import 'models.dart'; final gameStoreService = new InMemoryGameStoreService(); class InMemoryGameStoreService { final List<Game> games = [ new Game(1, "Darts", "Pub game", 'Darts is ...', "darts.jpg", 5), new Game(2, "Chess", "Board game", 'Chess is ...', "chess.jpg", 4), new Game(3, "Dices", "Random game", 'Dice are ...', "dice.jpg", 3), new Game(4, "Go", "Board game", 'Go is ...', "go.jpg", 2), new Game(5, "Poker", "Card game", 'Poker is ..', "poker.jpg", 4), new Game(6, "Pool", "Pub game", 'Pool is ..', "pool.jpg", 3), new Game(7, "Bingo", "Boring game", 'Bingo is ..', "bingo.jpg", 1) ]; }
-
Add a
games
attributes inx-games
class and instantiate it with games retrieved from the service:List<Game> games = gameStoreService.games;
- Iterate over the game list
-
Import
x-game
component in yourgames.html
file to be able to reused it. -
Loop over games with a template loop and reuse the
x-game
component like this (Hints):<x-game game="{{game}}"></x-game>
Hints:
- Try first to display only the names.
- The
game
attribute inx-game
should be a public attribute. (See Custom Attributes)