Git Product home page Git Product logo

adonisjs-petitevue-xstate's Introduction

AdonisJS + Petite Vue + XState

A little game to demonstrate how XState can be used front-end with a back-end framework such as Laravel, Ruby on Rails or AdonisJS, that renders plain HTML pages, and where JavaScript is only used to enhance them.

The board of the application

The Game

The green square needs to be moved to the red square to win the game. There are four buttons, to move the green square up, right, down and left.

When a game is won, another one can be started. The score is incremented.

The Stack

TL;DR

See XState machine →

See machine interpreting and petite-vue setup →

See template page →

Explanation

AdonisJS renders server-side a plain HTML page thanks to its template engine.

The logic of the game is driven by a XState machine.

petite-vue is used to add reactivity to the plain HTML page. We interpret the machine and we create a reactive object that contains the current state of the machine and a function to send events to it. This reactive object is attached to the petite-vue instance and can be used everywhere in the page.

The board is built server-side by AdonisJS, but styles for green and red squares are added client-side, according to values of the context of the machine.

Buttons are constructed server-side too, and in the expression for the @click attribute, executed when the button is clicked, we send an event to the machine, whose name is the label of the button, known server-side.

When the game is finished, a modal is displayed to ask if another game should be started. The modal is displayed when the current state is Won game. The game is restarted when the button on the modal is clicked.

Development

Follow those steps to launch the application on your computer:

  1. Clone the repository
  2. Install dependencies: npm install
  3. Create .env file at the root from the content of .env.example
  4. Launch AdonisJS development server: npm run dev
  5. Visit development server at localhost:3333

Credits

I thank all the contributors of the awesome tools I used for this project:

adonisjs-petitevue-xstate's People

Contributors

devessier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.