All Projects → ciscoheat → Mithril Hx

ciscoheat / Mithril Hx

Haxe externs for Mithril (JS MVC framework)

Programming Languages

haxe
709 projects

Projects that are alternatives of or similar to Mithril Hx

Azos
A to Z Sky Operating System / Microservice Chassis Framework
Stars: ✭ 137 (-23.03%)
Mutual labels:  mvc
Keera Hails
Keera Hails: Haskell on Rails - Reactive Programming Framework for Interactive Haskell applications
Stars: ✭ 153 (-14.04%)
Mutual labels:  mvc
Lad
👦 Lad is the best Node.js framework. Made by a former Express TC and Koa team member.
Stars: ✭ 2,112 (+1086.52%)
Mutual labels:  mvc
Interviews
A list of fancy questions I've been asked during the interviews I had. Some of them I ask when interviewing people.
Stars: ✭ 140 (-21.35%)
Mutual labels:  mvc
Hiboot
hiboot is a high performance web and cli application framework with dependency injection support
Stars: ✭ 150 (-15.73%)
Mutual labels:  mvc
Ihp
🔥 The fastest way to build type safe web apps. IHP is a new batteries-included web framework optimized for longterm productivity and programmer happiness
Stars: ✭ 2,746 (+1442.7%)
Mutual labels:  mvc
Denovel
A Deno Framework For Web Artisan - Inspired by Laravel
Stars: ✭ 128 (-28.09%)
Mutual labels:  mvc
Proteus
Lean, mean, and incredibly fast JVM framework for web and microservice development.
Stars: ✭ 178 (+0%)
Mutual labels:  mvc
Aspnetcore
ASP.NET Core Extension Library
Stars: ✭ 152 (-14.61%)
Mutual labels:  mvc
Cms
Modular CMS powered by CakePHP
Stars: ✭ 163 (-8.43%)
Mutual labels:  mvc
Miniphp
A small, simple PHP MVC framework skeleton that encapsulates a lot of features surrounded with powerful security layers.
Stars: ✭ 144 (-19.1%)
Mutual labels:  mvc
Carry
ClojureScript application framework.
Stars: ✭ 149 (-16.29%)
Mutual labels:  mvc
Estore
一个基于JavaWeb的网上电子购物城项目,实现展示商品、购买商品、提交订单、持久化保存到数据库等基本功能
Stars: ✭ 157 (-11.8%)
Mutual labels:  mvc
Studentmanagement
J2EE项目系列(一)--运用MVC模式及JavaWeb三层框架的学生管理系统。
Stars: ✭ 139 (-21.91%)
Mutual labels:  mvc
Diamond
Diamond is a full-stack web-framework written in The D Programming Language using vibe.d
Stars: ✭ 173 (-2.81%)
Mutual labels:  mvc
Digitalwand.admin helper
API для сборки кастомных админок в Битриксе
Stars: ✭ 135 (-24.16%)
Mutual labels:  mvc
Awesome Mithril
A curated list of Mithril awesome
Stars: ✭ 155 (-12.92%)
Mutual labels:  mithril
Serenity
Business Apps Made Simple with Asp.Net Core MVC / TypeScript
Stars: ✭ 2,168 (+1117.98%)
Mutual labels:  mvc
Woowahanjs
웹 어플리케이션 개발을 위한 JS프레임워크
Stars: ✭ 171 (-3.93%)
Mutual labels:  mvc
Mandarinets
Mandarine.TS is a typescript, decorator-driven framework that allows you to create server-side applications. Mandarine.TS provides a range of built-in solutions such as Dependency Injection, Components, ORM and more. Under its umbrella, Mandarine.TS has 4 modules: Core, Data, Security and MVC, these modules will offer you the requirements to build a Mandarine-powered application.
Stars: ✭ 161 (-9.55%)
Mutual labels:  mvc

Mithril for Haxe

Mithril is a small, yet great javascript MVC framework that is faster and more flexible than most others. Here's the Haxe version for Mithril 2, with some useful extra features thanks to macros and the type inference.

Installation

Standard procedure: haxelib install mithril and then -lib mithril in your .hxml file.

How to use

Mithril has a great introduction on its website and plenty of documentation, so I'll only highlight what you need to get started with the Haxe version here.

Implement the Mithril interface

When using Mithril, you will create components that will be used with the Mithril API. The recommended way to create a component is using a class that implements the Mithril interface. Here's an example of a Mithril component:

import mithril.M;

class TodoComponent implements Mithril
{
    var todos : Array<Todo>;

    public function new(todos) {
        this.todos = todos;
    }

    // When implementing Mithril, the last m() expression 
    // or Array of m() is returned automatically.
    public function view()
        m("div", [
            m("h1", "To do"),
            m("table", [for(todo in todos)
                m("tr", [
                    m("td", m("input[type=checkbox]", { 
                        onclick: e -> todo.done = e.target.checked,
                        checked: todo.done
                    })),
                    m("td", todo.description)
                ])
            ])
        ]);
}

/**
 * The model
 */
class Todo
{
    public var done : Bool;
    public var description : String;

    public function new(description, done = false) {
        this.description = description;
        this.done = done;
    }
}

class Main
{
    // Program entry point
    static function main() {
        var todos = [
            new Todo("Learn Haxe"),
            new Todo("??"),
            new Todo("Profit!")
        ];
        
        M.mount(js.Browser.document.body, new TodoComponent(todos));
    }
}

The major API differences

  • Use M, not m! import mithril.M;, then use M instead of m for the whole API. As you see above, the only exception is when using m(), you can use that without prefixing with M.
  • m.redraw.sync() is available through M.redrawSync().

Upgrading from 1.x to 2.x

  • The M.route methods can now be called as in the Mithril syntax, M.route.param etc. To call M.route however, use M.route.route.
  • M.withAttr has been removed. Use an e -> e.target lambda function instead.

When using Node.js

If you're using Node.js, you can install and use Mithril from npm instead of the Haxe port (see below for server side examples). To do that, define -D mithril-native.

Side note: "this" is slightly different in native javascript

Because of the slight mismatch between Haxe classes and the classless Mithril structure, in lifecycle methods, the native javascript this points to vnode.tag instead of vnode.state. Otherwise it would have pointed to another object when inside instance methods.

This is usually nothing you have to worry about if you're using Haxe classes for your components and state. In that context, this works as expected.

Haxe examples

This repo has some examples that can be interesting to test. Clone it, open a prompt in the directory and run:

haxelib install mithril

Then select one of the following:

Some small apps

A collection of two demo apps, available on the Mithril site.

  1. haxe client.hxml
  2. nekotools server -d bin
  3. Open http://localhost:2000/ in a browser.

Webshop

A simple e-commerce site to demonstrate the power of Mithril.

  1. haxe webshop.hxml
  2. nekotools server -d bin/webshop
  3. Open http://localhost:2000/ in a browser.

Live demo here: http://ciscoheat.github.io/webshop

From scratch

If you prefer a bare-bones example (doesn't require cloning), create the following two files and follow the instructions below:

index.html

<!doctype html>
<body>
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script src="example.js"></script>
</body>

Example.hx

import mithril.M;

class User
{
    public var name : String;

    public function new(name) {
        this.name = name;
    }
}

class Example implements Mithril
{
    var user : User;

    public function new() {
        this.user = new User("Thorin Oakenshield");     
    }

    public function view() [
        // Display an input field
        m('input', {
            // Updates the model on input
            oninput: e -> user.name = e.target.value,

            // The redraw triggered by the oninput event will update
            // the input field value from the model automatically
            value: user.name
        }),
        
        // Display a div with class .user and some style
        m('.user', {style: {margin: "15px"}}, user.name)
    ];

    // Program entry point
    static function main() {
        M.mount(js.Browser.document.body, new Example());
    }
}

Compile and run with:

  1. haxe -lib mithril -js example.js -main Example
  2. Open index.html in a browser.

Server side - All targets

The rendering part of Mithril has been ported to Haxe, so you can now enjoy writing Mithril templates and have them rendered to HTML anywhere. Here's a class to get you started:

import mithril.MithrilNodeRender;
import mithril.M.m;

class Main {
    static function main() {
        var view = m("ul", [
            m("li", "item 1"),
            m("li", "item 2"),
        ]);

        // <ul><li>item 1</li><li>item 2</li></ul>
        Sys.println(new MithrilNodeRender().render(view)); 
    }
}

(Note: The above code may not work in interp mode. Test it with neko instead.)

Server side - Node.js & isomorphism

Without too much hassle, it's possible to render a Mithril component/view serverside on Node.js. Run the following in the repo directory:

  1. npm install
  2. haxelib install hxnodejs
  3. haxe server.hxml
  4. cd bin

Example 1: Simple rendering

node server.js outputs a simple HTML rendering example.

Example 2: Isomorphic code

node server.js server

Starts a server on http://localhost:2000 that executes the same code on server and client. The server generates the HTML so the page is perceived to load quickly and search engines can index it, then the client enables the functionality.

Example 3: Cross-platform rendering

As a bonus, a Neko version of Example 1 will also be compiled. Test it with

neko server.n

The MithrilNodeRender is tested with travix and should work on all targets.

Feedback please!

Feedback is always welcome! Open an issue and give me a piece of your mind. :)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].