Skip to content

Instantly share code, notes, and snippets.

@pelonpelon
pelonpelon / index.js
Created November 13, 2014 02:54
requirebin sketch
// example using the raf module from npm. try changing some values!
var requestAnimationFrame = require("raf")
var canvas = document.createElement("canvas")
canvas.width = 500
canvas.height = 500
document.body.appendChild(canvas)
var context = canvas.getContext("2d")
@pelonpelon
pelonpelon / gist:d38e5fa698e1c9de7ca8
Created January 21, 2015 01:43
mithril.elements without module escape mechanism
/*
* Mithril.Elements
* Copyright (c) 2014 Phil Toms (@PhilToms3).
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
'use strict';
var m = (function app(window, mithril) {
@pelonpelon
pelonpelon / stringify.js
Created April 28, 2015 17:14
stringify.js - view objects as strings in the console
export default ( object, jsonIndent = 2 ) =>
JSON.stringify(
object, ( key, value ) =>
value instanceof Function
? value.toString()
: value,
jsonIndent
)
.replace( /\\n/g, '\n' )
.replace( /\\r/g, '\r' )
@pelonpelon
pelonpelon / finite_state_machine.js
Created April 29, 2015 22:08
Example finite state machine - credit: Nijiko Yonskai (https://github.com/Nijikokun)
function state (namespace) {
if (!namespace) return internalState[internalState.length - 1]
internalState.push([namespace, options])
m.redirect(namespace, options)
}
function goto (namespace, options) {
return function (e) {
e.preventDefault()
e.stopPropagation()
@pelonpelon
pelonpelon / mithril_component_inheritance.js
Created April 29, 2015 22:16
Mithril component inheritance - credit: Gilbert (https://github.com/mindeavor)
m.initComponent = function (component, options, content) {
var controller = new component.controller(options)
controller.render = function (options2, content2) {
return component.view(controller, options2 || options, content2 || content)
}
return controller
}
@pelonpelon
pelonpelon / mithril_unicode_and_whitespace.md
Last active May 11, 2017 16:17
Unicode, whitespace and HTML entities in Mithril views

####Unicode needs no extra escaping when in a view template string

m('div', "あなたは友人である場合は、パスワードを話す、とドアが開きます")
m.render(document.body, "hello 世界")

####Whitespace can be achieved a few different ways:

css

m("div[style='white-space:pre']", "LIST:\n\titem 1\n\titem 2")
@pelonpelon
pelonpelon / ajax_loading_indicator.js
Created May 15, 2015 00:17
Mithril: Background AJAX Loading Indicator
var LoadingExample = {
controller: function () {
var ctrl = this
// `background: true` is important here.
// Otherwise Mithril will wait for the AJAX request to complete before rendering the view.
ctrl.users = m.request({ url: 'users', method: 'GET', background: true })
},
view: function (ctrl) {
return m('.users', [
m('h3', "All Users"),