Skip to content

Instantly share code, notes, and snippets.

@bonza-labs
bonza-labs / knockout.autonumeric.js
Created November 5, 2011 09:09
Knockout.js and autoNumeric.js bindings
ko.bindingHandlers.autoNumeric = function ($) {
function getElementValue(el) {
return parseFloat(el.autoNumericGet(), 10);
}
function getModelValue(accessor) {
return parseFloat(ko.utils.unwrapObservable(accessor()), 10);
}
@bonza-labs
bonza-labs / knockout.autonumeric.js
Created November 5, 2011 09:08
Knockout.js and autoNumeric.js bindings
ko.bindingHandlers.autoNumeric = function ($) {
function getElementValue(el) {
return parseFloat(el.autoNumericGet(), 10);
}
function getModelValue(accessor) {
return parseFloat(ko.utils.unwrapObservable(accessor()), 10);
}
@bonza-labs
bonza-labs / curry.js
Created September 19, 2011 16:56
Javascript curry
Function.prototype.curry = function () {
var method = this, args = Array.prototype.slice.call(arguments);
return function () {
return method.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
};
@bonza-labs
bonza-labs / 05_dom_events.html
Created September 16, 2011 18:45
DOM events exercise
<p>1. Change the addEventListener calls so that the events occur in the following order.<br/>
the_div! the_item! the_list!</p>
<div id="the_div">
<ul id="the_list">
<li id="the_item">Click me! 1</li>
</ul>
</div>
<p>2. Change the addEventListener calls so that the events occur in the following order.<br/>
the_item! the_list! the_div!</p>
<div id="the_div2">
// 1. Write a class to support the following code:
var Person = function(name) {
this.name = name;
}
var thomas = new Person('Thomas');
var amy = new Person('Amy');
thomas.name // --> "Thomas"
@bonza-labs
bonza-labs / exercise_01.js
Created September 15, 2011 23:35
OO javascript
var Car = function(make, color){
this.make = make;
this.color = color;
this.log = function(){
console.log('I am a '+ this.color +' '+ this.make);
};
}
// Example call for OO version:
@bonza-labs
bonza-labs / caching_wrapper.js
Created September 15, 2011 23:34
Javascript cache wrapping
var $w = function(string) { return string.split(' '); };
var asInts = function(arr) {
var results=[];
for(var i=0, m=arr.length; i<m; i++) results.push(parseInt(arr[i], 10));
return results;
};
Array.prototype.map = function(func) {
@bonza-labs
bonza-labs / gist:1220112
Created September 15, 2011 18:52
Scoping example using closure
(function(scope) {
// Code
var index;
function log(){
console.log(index);
};
function iterate(){
log();
@bonza-labs
bonza-labs / console-stub.js
Created August 30, 2011 19:48
Stub the widow.console object when not available
if (typeof window.console === "undefined") {
var noOpFunc = function() { };
window.console = {
assert: noOpFunc,
log: noOpFunc,
warn: noOpFunc,
debug: noOpFunc,
info: noOpFunc,
time: noOpFunc,
@bonza-labs
bonza-labs / traffic.html
Created August 25, 2011 23:42
Google maps API example showing Seattle traffic.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>