Code Monkey home page Code Monkey logo

elm-html-and-js's Introduction

HTML/JS integration โ€” live demo

This project illustrates how to embed an Elm program in an HTML page and how to communicate with JavaScript.

Build Instructions

git clone https://github.com/evancz/elm-html-and-js.git
cd elm-html-and-js
elm-make Stamps.elm --output elm.js
open index.html

Overview of API Usage

If you create an Elm module named Stamps, it will be named Elm.Stamps in JavaScript. To instantiate a module you can use any of the following functions:

Elm.fullscreen(Elm.Stamps, {reset:[]});   // take over the whole page
Elm.embed(Elm.Stamps, div, {reset:[]});   // embed in a specific DOM node
Elm.worker(Elm.Stamps,     {reset:[]});   // instantiate without graphics

Each of these creates a module instance that you can communicate with from JavaScript.

// Embed the Stamps module in a div with ID 'elm-stamps'
var div = document.getElementById('elm-stamps');
var stamps = Elm.embed(Elm.Stamps, div, {reset:[]});

// You can send and receive values through
// ports 'reset' and 'count'.
stamps.ports.reset.send([]);
stamps.ports.count.subscribe(function(event) {
    console.log(event.value);
});

Communication between Elm and JavaScript happens by sending events through ports, like reset and count. Elm interprets these ports as signals and JavaScript interprets them as event streams. In Elm, you declare ports like this:

-- incoming reset events
port reset : Signal ()

-- outgoing count of stamps
port count : Signal Int
port count = length <~ stamps

Values passed along named channels must conform to these rules.

elm-html-and-js's People

Contributors

process-bot avatar kasbah avatar mkoconnor avatar basti1302 avatar thsoft avatar hossameldeen avatar

Watchers

 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.