Giter VIP home page Giter VIP logo

crazy-taxi's Introduction

Crazy Taxi

Universal Javascript: easy and seamless


Crazy taxi is based on Mithril.js and uses hyperscript syntax (pure JS) for it's templates. Dead simple backend rendering.

var ct = require('crazy-taxi’)

var component = ct.compile('./component.js’)

var compiledHTMLString = component({name: ‘Joe’})

The rendering of each component is independent, and functions independently on the front end. Any attributes you pass into the compiled function are pre-rendered and hydrated automatically on the client!

var rendered_page = `
	<head>
		<title>Craaaaazy Taaaaxi</title>
	</head>

	<body>
		<h1>
			${component_one({name: ‘Joe’})}
			${component_one({name: ‘Schmoe’})}
		<h2>

		<p>
			${component_two({title: ‘Programmer’})}
		</p>

		<div>
			<p>
				Don't be afraid to write HUUUUGE components!
			</p>
			${huge_component_with_lots_of_children()}
		</div>
	</body>
`

Template Engine:

Crazy Taxi's tempating is powered by Mithril.js A super tiny (7kb gzip & min), ultra fast library that is extremely easy to learn.

Mithril Uses Hyperscript templating, which unlike JSX is pure javascript so it's easy to reason about and requires no transpilation:

var SimpleMarkupComponent = {
    view: function(vnode) {
        return c('div', [ 
	        c('p', {style:{'color': 'orange'}} ,'Hello!')
        ]
    }
}

Just like React, Mithril uses a virtual DOM to diff changes and redraw the page. This means is has lifecycle components, just like react:

var ComponentWithHooks = {
    oninit: function(vnode) {
        console.log("initialized")
    },
    oncreate: function(vnode) {
        console.log("DOM created")
    },
    onupdate: function(vnode) {
        console.log("DOM updated")
    },
    onbeforeremove: function(vnode, done) {
        console.log("exit animation can start")
        done()
    },
    onremove: function(vnode) {
        console.log("removing DOM element")
    },
    onbeforeupdate: function(vnode, old) {
        return true
    },
    view: function(vnode) {
        return "hello"
    }
}

Options:

Once a Crazy Taxi component has been compiled, you can pass options into the returned function:

Options:

  • exclude_framework: Bool Removes the the compiled Mithril framework from the output string. By default Crazy Taxi will include Mithril in every output string generation. If you have more than one component being sent from the server at a time, you can set exclude_framework: true for every component after the first and just let them piggy back of the first components framework instance.

Examples:

Basic:

/component.js:

var c = require('crazy-taxi')

module.exports = {
   
   // This is the view. It renders hyperscript.
    view: function(vnode) {
        return c('h1', vnode.attrs.text)
    }
}

/server.js

const express 		= require('express')
const c 			= require('crazy-taxi')

const app = express()

const component = c.compile('./component.js')

app.get('*', (req, res) => {

	res.send(component({text: 'Hello World!'}))

})


app.listen(3000)

Easy:

/component.js:

var c = require('crazy-taxi')

const Component = {

    oninit: function(vnode) { 
    
        vnode.state.name = vnode.attrs.name

        vnode.state.randomName = function(e){
            var name_array = [
                'Benedict Kapusniak',
                'Leota Wetsel',
                'Bryon Caselli',
                'Karin Nersesian',
                'Monroe Soprych',
                'Alfonso Noerenberg',
            ]
            
            vnode.state.name = name_array[Math.floor(Math.random() * name_array.length)]
        }

   
        vnode.state.async_response = c.request({
            method: "GET",
            url: "https://jsonplaceholder.typicode.com/posts",
            initialValue: []
        })

    },
   
    view: function(vnode) {
        return c('div', [

        	c('h1', ['Hello ' + vnode.state.name]),

            c('button', {onclick: vnode.state.randomName}, 'Say Hi!'),

            c('pre', JSON.stringify(vnode.state.async_response().slice(0, 4), null, 4))
        ])
    }
}

module.exports = Component 

/server.js

const express 		= require('express')
const ct 			= require('crazy-taxi')
const random_name 	= require('node-random-name')


const app = express()


const asyncFunction = () => new Promise((resolve, reject) => {
	
	setTimeout(() => {

		resolve(random_name())

	}, 100)
})



const component = ct.compile('./component.js')

app.get('*', (req, res) => {

	asyncFunction().then(name => {

		res.send(`

			${component({name: name})}

			${component({name: 'Joe Schmoe'}, {exclude_framework: true})}
		`)
	})

})


app.listen(3000)

Universal Javascript: Finally Easy

crazy-taxi's People

Contributors

augustbrenner avatar pikeas avatar

Watchers

 avatar James Cloos 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.