Skip to content

Instantly share code, notes, and snippets.

@karlwestin
karlwestin / gist:1756183
Created February 7, 2012 00:31
Overriding set-method in Backbone.js models
set: function(attrs, options) {
_.each(attrs, function(val, key) {
if(!!this.validations[key])
attrs[key] = this.filters[ this.validations[key].filter ](val);
}, this);
Backbone.Model.prototype.set.call(this, attrs, options);
return this;
},
@karlwestin
karlwestin / gist:1779676
Created February 9, 2012 12:30
Trying a potention performance improvement in the Actor.animation-function in CAAT
animate : function(director, time) {
// General idea:
// try to touch the DOM as little as possible,
// extra JS calculations much cheaper than extra DOM-settings
if ( !this.isInAnimationFrame(time) ) {
this.inFrame= false;
this.dirty= true;
this.style( 'display', 'none');
@karlwestin
karlwestin / gist:2000461
Created March 8, 2012 11:05
Bookmarklet for JSBin.com line numbers
// Since Posterous don't support javascript:
// Just add whatever link as bookmark, and paste this as the link instead
javascript:(function() { editors.javascript.setOption("lineNumbers", true); editors.html.setOption("lineNumbers", true); })();
@karlwestin
karlwestin / gist:2045042
Created March 15, 2012 16:17
m.OnceBehavior
(function(m) {
m.OnceBehavior = function() {
m.OnceBehavior.superclass.constructor.call(this);
};
m.OnceBehavior.prototype = {
setForTime : function(time, actor) {
if(this.oldtime && this.oldtime > time)
this.callback();
@karlwestin
karlwestin / gist:2281472
Created April 2, 2012 07:50
Installing jshint via npm on MacOS X Lion
Are you also having problems installing jshint (or other module) on Lion:
this is what i saw:
npm ERR! Error: ENOENT, chmod
... and loads of other stuff
This worked for me:
1. Clone the node-jshint repo
@karlwestin
karlwestin / gist:2375986
Created April 13, 2012 11:15
Partial application javascript
var partial = function(func) {
return Function.prototype.bind.apply(func, arguments);
};
function add() {
var x = 0;
[].forEach.call(arguments, function(el) {
x += el;
@karlwestin
karlwestin / gist:2393106
Created April 15, 2012 14:22
Nested models/collections and storage with Backbone.js and Backbone.LocalStorage
/*
* Example of using nested collections/models with backbone.js and backbone.js localStorage
*
* Read the complete blog post at karlwestin.posterous.com
*
* 1. run populate() from command line first time
* 2. reload the page (or open in another browser tab), and check the following:
*
* premier.at(0).get("name") => "Tottenham"
* premier.at(0).players.length => 3
@karlwestin
karlwestin / gist:2688283
Created May 13, 2012 12:42
Refactoring JS for testing part 1.
/*
* Karl Westin
* Part of the "refactoring javascript for unit testing" blog post
*/
$(document).ready(function() {
$("#boxlink").bind("click", function(e) {
var content = "<div class='js-overlay'></div>" +
"<div class='js-dialog'>" +
@karlwestin
karlwestin / gist:2690491
Created May 13, 2012 22:20
Refactoring JS for testing part 2
/*
* Karl Westin
* Part of the "refactoring javascript for unit testing" blog post
*/
function Lightbox(boxcontent) {
var content = "<div class='js-overlay'></div>" +
"<div class='js-dialog'>" +
"<a href='#' class='js-close'>Close</a>" +
"<div class='js-content'>" +
@karlwestin
karlwestin / gist:2690494
Created May 13, 2012 22:23
Refactoring JS for testing part 3 – adding tests
describe("Lightbox tests", function() {
beforeEach(function() {
var content = "<div style='width: 100px; height: 110px; position: relative; display: inline-block; background-color: #fff;'>My test-content</div>"
// for unit-testing purposes, we turn jQuery.fx off
$.fx.off = true;
setFixtures(sandbox());
this.$sandbox = $("#sandbox");