Skip to content

Instantly share code, notes, and snippets.

View bgerrissen's full-sized avatar

Ben Gerrissen bgerrissen

View GitHub Profile
// some base class
var Dispatcher = function(){
this._listeners = [];
}
Dispatcher.prototype = {
listen: function(){},
deafen: function(){},
notify: function(){}
}
@bgerrissen
bgerrissen / js-loader.js
Created May 24, 2011 09:05
Robust cross browser JS Loader for dependency management.
/**
* Author: Ben Gerrissen
* Date: 9-3-11
* License: MIT
*/
(function( global ){
var undefined
, preloads = false
// you know
require();
define();
// next step
compose( "scopeID" , [ "extra" ], function( src , extra ){
// all options
src( "some/module" ) // a js class or object
@bgerrissen
bgerrissen / gist:816731
Created February 8, 2011 16:50
UA/Feature detection Service

If UA (server side) is unknown, return feature detection kit that sets features object client side and sends results back to server so the server can cache it.

Any consequtive request for that specific UA will return a much smaller server cached features object script.

Lets say (theoretically) the full feature test script is 50kb and a cached feature set script 10kb. The first ever request for an unique UA will be a hit for 50kb and every other request after that (for that specific UA) for just 10kb.

For a high traffic website this can offer substantial savings.

Also, after saving say 20 unique UA feature tests, the service will also become rather static as 80 to 90% of the audience is covered. No biggy, the service will be useful still to feature test future browsers.

// pluginDetection
// don't put this under a too global namespace object aka. "support"
define( [] , function () {
var pluginDetection = {}
// SILVERLIGHT
, getSilverlightVersion = function(){}
, silverlight = function ( version ) {
@bgerrissen
bgerrissen / gist:715141
Created November 25, 2010 09:52
RequireJS cacheKey

reason

Offers a simple mechanism to allow extremely long expire-headers on scripts whilst still being able to switch to newer versions on the fly.

implementation

A js file defined with data-main attribute on the script tag of require.js, will always be loaded with current timestamp to always force a fresh load.

// main.js?cacheKey=TIMESTAMP-123412312321
@bgerrissen
bgerrissen / gist:713629
Created November 24, 2010 13:09
Record mechanism Require JS (modulejs variant)

Record mechanism Require JS (modulejs variant)

Record object

{
    path : "foo/bar",         // final path/id
    needs : [],               // should stay intact
    fetch : [],               // each dependency is destroyed once it's completed
    status : 0,               // module status; new, loading, pending, complete, failed

context : "http://etc", // root dir containing path

Event mechanism Require JS (modulejs variant)
*Methods*
- require.listen( eventType , listener )
- require.deafen( eventType , listener )
- require.notify( eventType , dataObject , messageString )
*Events*
@bgerrissen
bgerrissen / gist:713430
Created November 24, 2010 10:06
Require JS Path Expressions

Concept Path Expressions
see https://github.com/bgerrissen/modulejs for partial implementation.

Based on plugin system from RequireJS.

rules

  • Expressions are always placed behind the path.
  • Whitespace can be applied liberally.
  • Params are optional and parenthesis can be omitted.
DOM Queries
-jQuery-esque DOM element collection sugar.
+Find elements by selector and receive a collection object with chainable methods for fast and efficient DOM handling.