Skip to content

Instantly share code, notes, and snippets.

View thomasboyt's full-sized avatar

Thomas Boyt thomasboyt

View GitHub Profile
@thomasboyt
thomasboyt / gist:5703635
Created June 4, 2013 04:42
Fixing CommonJS compiling in ES6 module transpiler

The Problem

The various ES6-based microlibraries I've been working on recently have the following file structure:

lib/
  foo/
    bar.js
    baz.js
  foo.js
concat: {
amd: {
src: "tmp/**/*.amd.js",
dest: "dist/my_library.amd.js"
}
}
@thomasboyt
thomasboyt / bindAttr.js
Created June 13, 2013 16:11
angular bindAttr
module.directive("bindAttr", function() {
return function(scope, element, attrs) {
var args = JSON.parse(attrs.bindAttr);
for (var key in args) {
scope.$watch(args[key], function() {
var val = scope.$eval(args[key]);
attrs.$set(key, val);
}, true);
}
}
@thomasboyt
thomasboyt / neuter.js
Created June 21, 2013 15:49
neuter.js retabbed
/*
* grunt-neuter
*
* Copyright (c) 2012 Trek Glowacki, contributors.
* Licensed under the MIT license.
*/
'use strict';
var glob = require("glob");
@thomasboyt
thomasboyt / syntax.js
Last active July 2, 2016 01:13
es6 module blog post
// ES6 includes a new block keyword, "module", for defining modules
// in-line.
module "myModule" {
export default function() {
// You can export classes, functions, and other blocks.
return "Hello :)";
}
// You can mix a default and named exports.
@thomasboyt
thomasboyt / observe.html
Last active December 19, 2015 16:48
proxies vs observers
<!-- Works in Chrome with "Experimental JavaScript Features" flag enabled -->
<html>
<body>
<span id="test"></span>
<script>
var foo = {};
Object.observe(foo, function(records) {
records.forEach(function(record) {
document.getElementById(record.name).innerHTML = record.object[record.name];
@thomasboyt
thomasboyt / defaultdict.js
Last active February 18, 2016 19:22
defaultDict with ES6 proxies. Firefox only!
function defaultDict(default_) {
return new Proxy({}, {
get: function(target, name, receiver) {
// iterator and next are two JS1.7 things Firefox looks for when you
// log an object to the console. pretty sure it's a bug that they trigger
// the get trap.
if (!(name in target) && !(name == '__iterator__'|| name == 'next')) {
target[name] = default_();
}
return target[name];
@thomasboyt
thomasboyt / batch.js
Last active December 19, 2015 19:59
js getters and setters
var o = {};
Object.observe(o, function(records) {
console.log(records);
});
// the following three changes are batched, as they occur
// within the same frame of the loop
o.x = "foo";
o.y = "bar";
@thomasboyt
thomasboyt / findby.js
Created July 17, 2013 14:42
weird rails-y findBy method in JS
var myClassCollection = [];
var _MyClass = function(id, foo) {
this.id = id;
this.foo = foo;
// some sort of arbitrary storage thing
this.collection[id] = this;
}
post = models.Post.build({
title: 'first post'
date: Date.now()
body: 'This is an autogenerated post'
})
post.save().error((err) ->
console.log 'Error while saving: ' + err
).success(() ->
console.log 'Saved properly'