Skip to content

Instantly share code, notes, and snippets.

@fauxparse
fauxparse / sortable.coffee
Created January 13, 2015 01:49
Sortable lists in < 100 LoC
class SortableList
constructor: (el) ->
@el = $(el)
down: (e) =>
e.preventDefault()
top = @el.offset().top
@el.find(".sortable").each (i, el) ->
$el = $(el)
offset = $el.offset().top
pathological.js: line 1, col 17, Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.
pathological.js: line 1, col 17, Expected an assignment or function call and instead saw an expression.
pathological.js: line 2, col 17, Move the invocation into the parens that contain the function.
@fauxparse
fauxparse / polling.example.js
Created May 3, 2012 02:57
Easy polling in jQuery
$.poll({
url: '/status.json'
, dataType: 'json'
, timeout: 1000 // initial timeout of 1s
, multiplier: 2 // double the timeout each time
, retries: 10 // maximum of ten tries
, until: function(data) { return data.status == 'done'; }
, success: function(data) { alert(data.message); }
});
@fauxparse
fauxparse / messaging.coffee
Created January 17, 2012 22:16
On-screen messaging for Spine + Zepto
class Messages extends Spine.Controller
constructor: ->
super
@el.addClass("messages")
@appendTo "body"
send: (str) ->
@append(new Message({ message: str }))
@instance: ->
@fauxparse
fauxparse / date.coffee
Created December 21, 2011 23:20
CoffeeScript date utilities
Number::pad = (digits, signed) ->
s = Math.abs(@).toString()
s = "0" + s while s.length < digits
(if @ < 0 then "-" else (if signed then "+" else "")) + s
Date.months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
Date.weekdays = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
Date.formats =
"a": -> Date.weekdays[@getDay()].substring(0, 3)
@fauxparse
fauxparse / 1.html
Created August 9, 2011 18:29
JS Masterclass Ex 8
<div id="the_div">
<ul id="the_list">
<li id="the_item">Click me!</li>
</ul>
</div>
<p id="log"></p>
<script type="text/javascript" charset="utf-8">
function log(string){
@fauxparse
fauxparse / 08_inheritance.js
Created August 9, 2011 17:12
JS Masterclass Exercise 8
// 1. Write a class to support the following code:
function Person(name) {
this.name = name;
}
var thomas = new Person('Thomas');
var amy = new Person('Amy');
thomas.name // --> "Thomas"
@fauxparse
fauxparse / cached.js
Created August 9, 2011 16:23
Multi-param memoization
Function.prototype.cached = function() {
var f = this,
cache = {};
function get(c, a, i) {
var l = a.length, k = a[i];
if (l == 0) {
return f();
} else if (i==l) {
return (k in c) ? c[k] : (c[k] = f.apply(this, a));
@fauxparse
fauxparse / part1.js
Created August 8, 2011 20:03
JS Masterclass homework
function logCar(options) {
if (options.hasOwnProperty('make') && options.hasOwnProperty('color')) {
console.log("I'm a " + options.color + " " + options.make);
} else {
console.log("Please supply make and color!");
}
}
function Car(make, color) {
this.make = make;
@fauxparse
fauxparse / 02_add_closure.js
Created August 8, 2011 18:46
JS Master Class Ex 2
// Exercise 2 - Closures
// Wrap the following code in a closure and export only the "countdown" function.
(function(scope, functionName) {
var index;
function log(){
console.log(index);
}