0.4.9 • Published 6 years ago

crazy-taxi v0.4.9

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.35

6 years ago

0.3.34

6 years ago

0.3.33

6 years ago

0.3.32

6 years ago

0.3.31

6 years ago

0.3.30

6 years ago

0.3.29

6 years ago

0.3.28

6 years ago

0.3.27

6 years ago

0.3.26

6 years ago

0.3.25

6 years ago

0.3.24

6 years ago

0.3.23

6 years ago

0.3.22

6 years ago

0.3.21

6 years ago

0.3.20

6 years ago

0.3.19

6 years ago

0.3.18

6 years ago

0.3.17

6 years ago

0.3.14

7 years ago

0.3.13

7 years ago

0.3.12

7 years ago

0.3.11

7 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.16

7 years ago

0.1.15

7 years ago

0.1.14

7 years ago

0.1.13

7 years ago

0.1.12

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago