Skip to content

Instantly share code, notes, and snippets.

@webcss
webcss / eventsmixin.js
Last active August 9, 2016 12:44
PubSub mixin using CustomEvents
// utilizes the browser eventsystem
// usefull in cases where you need communication between independent components
// registered events are automatically removed onunload with preserving any other onunload handler
var eventsMixin = function(target) {
var _subscriptions = [];
target.broadcast = function(type, payload) {
var ev = new CustomEvent(type, {
detail: payload,
@webcss
webcss / firebasemixin.js
Last active April 5, 2016 09:36
Mixin for mithril controllers to enable firebase livedata
/******************************************
* Firebase mixin
******************************************/
export function mixinFirebase(target) {
var OBJECT = '[object Object]', ARRAY = '[object Array]', STRING = '[object String]', FUNCTION = '[object Function]';
var type = {}.toString;
var _references = [];
function unify(key, value) {
@webcss
webcss / fireactive.js
Last active October 6, 2016 17:10
Firebase.com with Mithril - the functional way
var fireactive = function(controller) {
return function(args) {
var instance = {};
var _ref = args.firebase;
instance.ref = _ref;
if (args.asObject) {
_ref.on('value', function asObject(snap) {
@webcss
webcss / defineDOM.js
Last active September 28, 2015 12:27
m.dom.<Tag/Component>
var HTMLTags = ['A', 'DIV', 'SECTION', 'HEADER', 'FOOTER', 'P',....];
m.dom = {};
m.defineDOM = function defineDOM(elementName, component, conf) {
// register a html tag, no special component view given
if (!component) {
elementName = elementName.toLowerCase();
m.dom[elementName] = function(attrs, children) {
return m(elementName, attrs, children);
@webcss
webcss / component.js
Last active August 29, 2015 14:14
mithril component implementation (adapted from James Long - Bloop)
m.createComponent = function(proto) {
function _component(attrs, children) {
// always return a fresh instance, allows for call without new
if(!(this instanceof _component)) {
return new _component(attrs, children);
}
if(!children && Array.isArray(attrs)) {
children = attrs;
@webcss
webcss / mithril-touch.js
Last active May 26, 2020 15:34
mithril-touch, consume touch and mouse events evenly with mithril
/*****************************************
/* DOM touch support module
/*****************************************/
if (!window.CustomEvent) {
window.CustomEvent = function (event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
};
@webcss
webcss / databinding.js
Last active April 6, 2016 07:47
Firebase with mithril. Note that the gists provided here are deprecated and will soon be removed. You should use firebaseMixin (https://gist.github.com/webcss/e4aaa7d95342d107e1ce) instead.
var Dinosaurs = {
model: function() {
// return a firebase reference to our database
return new Firebase('https://dinosaur-facts.firebaseio.com');
},
controller: function() {
var ref = Dinosaurs.model();
this.facts = mithrilFire(ref.orderByChild('height'));
},
view: function(ctrl) {
@webcss
webcss / firedemo.js
Last active April 8, 2019 23:46
Firebase client for Mithril
// mithrilFire usage demo
// using firebase database "dinosaur-facts"
var Dinosaurs = {
model: function() {
// return a firebase reference to our database
return new Firebase('https://dinosaur-facts.firebaseio.com');
},
controller: function() {
var data = Dinosaurs.model();