Skip to content

Instantly share code, notes, and snippets.

@peol
peol / docs.md
Created March 9, 2012 16:54 — forked from addyosmani/docs.md
basket.js docs

##basket.js 0.2

###Introduction

basket.js is a small JavaScript library supporting localStorage caching of scripts. If script(s) have previously been saved locally, they will simply be loaded and injected into the current document. If not, they will be XHR'd in for use right away, but cached so that no additional loads are required in the future.

###Why localStorage?

Tests by Bing and Google have shown that there are performance benefits to caching assets in localStorage (especially on mobile) when compared to simply reading and writing from the standard browser cache. This project is currently working on replicating these tests in the public so that we have definitive statistics on whether this is true.

@peol
peol / qunit.md
Created February 26, 2012 08:39
QUnit/SinonJS/Backbone.js

#Backbone.js Testing With QUnit & SinonJS

QUnit is a powerful JavaScript test suite written by Jörn Zaefferer and used by many large open-source projects (such as jQuery and Backbone.js) to test their code. It's both capable of testing standard JavaScript code in the browser as well as code on the server-side (where environments supported include Rhino, V8 and SpiderMonkey). This makes it a robust solution for a number of use-cases.

Quite a few Backbone.js contributors feel that QUnit is a better introductory framework for testing if you don't wish to start off with Jasmine and BDD right away. As we'll see later on in this chapter, QUnit can also be combined with third-party solutions such as SinonJS to produce an even more powerful testing solution supporting spies and mocks, which some say is preferable over Jasmine.

My personal recommendation is that it's worth comparing both frameworks and opting for the solution that you feel the most comfortable with.

@peol
peol / scraps.md
Created September 14, 2011 17:28 — forked from addyosmani/scraps.md
Scraps

/*Copyright Addy Osmani, 2011. Permission is not granted to re-publish this content unless otherwise stated. Its a work in progress :) */

#Essential jQuery Plugin Patterns (WIP)

##Introduction

I occasionally write about implementing design patterns in JavaScript. They're an excellent way of building upon proven approaches to solving common development problems and I think there's a lot of benefit to using them. That said, whilst well-known JavaScript patterns are useful, there's another side of development that can benefit from it's very own set of design patterns - jQuery plugins. The official jQuery plugin authoring guide offers a great starting point for getting into writing plugins and widgets, but let's attempt to take this further.

Plugin development has evolved over the past few years. We no longer have just one way of writing plugins, but many. They can be complex, they can be challenging but at the end of the day they can still be beautiful. I began to think about plugin patterns after seeing a n

@peol
peol / codereview.md
Created August 25, 2011 15:43 — forked from addyosmani/codereview.md
Lessons from a JavaScript code review

#Lessons From A JavaScript Code Review

I was recently asked to review some code and thought I might share some of the feedback I provided as it includes a mention of JavaScript fundamentals that are always useful to bear in mind.

####Problem: Functions to be used as callbacks (as well as objects) are passed as parameters to other functions without any type validation.

Feedback: For functions, at minimum 1) test to ensure the callback exists and 2) do a typeof check to avoid issues with the app attempting to execute input which may not in fact be a valid function at all.

if (callback && typeof(callback) === "function"){
$(function() {
// Clone HTML5's placeholder attribute functionality
$('input[placeholder]').each(function() {
// Make sure we cache the selector to fasten up the process
var $t = $(this);
// Store the placeholder text and then set the element's value
// TODO: Check to make sure value is empty before setting here
$t.data( 'placeholder', $t.attr('placeholder') );
$t.val( $t.data('placeholder') ).addClass('placeholder');