Skip to content

Instantly share code, notes, and snippets.

@domenic
domenic / critique.md
Created February 17, 2012 23:14
Critique of Giordan's page

More or less in decreasing order of importance:

  • [rel is misused][1]. Instead of <span rel="x"> it should be <a href="#x">; use <a href="#"> for the undecided ones. You could start using [the hashchange event][8] too to make things a bit nicer.
  • [<aside> is not used semantically][2].
  • Put "use strict"; at the top of your JavaScript.
  • That would give you errors if you did something silly, like, say, forgetting a var and thus creating a global variable targ or scrollTo.
  • preventDouble is not used.
  • In fact, your code could use some JSHinting for both [correctness][6] and [style][7].
  • The section#decisionList seems to be silly wrapper used only for styling; get rid of it.
  • JavaScript at the bottom of the page (before </body>) for best performance. This would also get rid of the need for the $(document).ready wrapper, but of course you'd still want to use an [IIFE][5] to avoid polluting the global scope.
@domenic
domenic / observable-from-property.js
Created February 24, 2012 21:28
Knockout observableFromProperty
var ko = require("knockout");
// TODO both of these leak memory :(. Neither subscription ever goes away.
exports.observableFromProperty = function (object, propertyName) {
var observable = ko.observable(object[propertyName]);
observable.subscribe(function (newValue) {
object[propertyName] = newValue;
});
@domenic
domenic / optionsHash.js
Created February 27, 2012 22:35
Attempt at an `optionsHash` binding for Knockout.js
ko.bindingHandlers.optionsHash = {
update: function (element, valueAccessor, allBindingsAccessor) {
var hash = ko.utils.unwrapObservable(valueAccessor());
var asArray = Object.keys(hash).map(function (k) {
return {
key: k,
val: hash[k]
};
});
@domenic
domenic / knockout-date.coffee
Created March 9, 2012 16:59
Knockout date binding
ko.bindingHandlers.date =
update: (element, valueAccessor) ->
theDate = ko.utils.unwrapObservable(valueAccessor())
if theDate? and theDate not instanceof Date
throw new TypeError("The date binding must be bound to a Date object on the view model.")
isJustDateNotDateTime = theDate?.getUTCHours() is 0 and theDate?.getUTCMinutes() is 0 and theDate?.getUTCSeconds() is 0 and theDate?.getUTCMilliseconds() is 0
isoDateTimeString = theDate?.toISOString()
isoDateString = isoDateTimeString?.substring(0, 10)
@domenic
domenic / coveraje.js
Created March 16, 2012 15:11
Drop-in coveraje
"use strict";
var coveraje = require("coveraje");
var fs = require("fs");
var path = require("path");
var rel = path.relative(process.cwd(), __dirname);
function moduleId(filename) {
return filename.slice(0, -1 * ".js".length);
@domenic
domenic / streams.js
Created March 19, 2012 19:35
Streams are painful
function existsAsync(id) {
var deferred = Q.defer();
var clientRequest = knoxClient.head(id);
clientRequest.on("error", deferred.reject);
clientRequest.on("response", function (clientResponse) {
function onEnd() {
cleanup();
deferred.resolve(clientResponse.statusCode === 200);
}
@domenic
domenic / upload.js
Created March 20, 2012 17:13
File upload using XHR PUT
(function () {
var $dropTarget = $("#drop-target");
var slice = Function.prototype.call.bind(Array.prototype.slice);
$dropTarget.on({
drop: function (event) {
slice(event.originalEvent.dataTransfer.files).forEach(putFile);
return false;
@domenic
domenic / README.md
Created March 29, 2012 16:01
Cross-platform git hooks for Node.js

Here's how this works:

  • Include a git_hooks/ directory in your project, with these two files (plus other hooks if you want, written in a similar style).
  • Add "npm" to your devDependencies in package.json, so that the pre-commit hook can do its magic.
  • Add test and lint scripts to your package.json, e.g.
    "scripts": {
        "test": "mocha",
 "lint": "jshint ./lib --show-non-errors"
@domenic
domenic / hal-catalog.json
Created April 10, 2012 18:25
HAL version of catalog
{
"title": "Catalog for Domenic, page 1",
"description": "Contains lots of books, which is cool",
"_links": {
"self": { "href": "/docs?page=1" },
"next": { "href": "/docs?page=2" }
},
"_embedded": {
"http://rel.nkstdy.co/document": [
{
@domenic
domenic / mocha-unexpected-async.js
Created April 13, 2012 17:43
Mocha unexpected async behavior
describe("Outer", function () {
var promise = null;
beforeEach(function () {
setTimeout(function () {
console.error("Outer beforeEach");
}, 0);
});
describe("Inner A", function () {