Skip to content

Instantly share code, notes, and snippets.

@cybrown
cybrown / javascript_file.js
Created June 15, 2013 10:45
New JavaScript base file.
(function () {
'use strict';
// Code here...
})();
@cybrown
cybrown / requirejs_module.js
Created June 15, 2013 11:03
RequireJS blank module with dependencies.
define(['dep1', 'dep2'], function (Dep1, Dep2) {
'use strict';
// Use Dep1 and Dep2
// return module var
});
@cybrown
cybrown / main.js
Created June 15, 2013 11:08
Base RequireJS app
require(['module1', 'module2'], function (Module1, Module2) {
'use strict';
// Main Application code here, use Module1 and Module2
});
@cybrown
cybrown / app.js
Created June 16, 2013 20:55
Node.js + express + socket.io base project.
;(function () {
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser('cookieparsersecret'));
@cybrown
cybrown / module.js
Created July 8, 2013 20:44
Require.js, nodejs and vanilla module definition (jQuery like).
;(function (window, undefined) { // undefined as an undefined parameter
'use strict';
var Module = null; // module definition here
if (typeof module === "object" && module && typeof module.exports === "object") {
module.exports = Module; // commonjs module (ndoejs...)
} else if (typeof define === "function" && define.amd) {
define([], function () {return Module;}); // amd module (requirejs...)
} else if (typeof window === "object" && typeof window.document === "object") {
@cybrown
cybrown / .gitignore
Created July 14, 2013 16:24
Basic testable Silex application with travis, phpunit, composer and gitignore files.
/composer.lock
/vendor
@cybrown
cybrown / CustomControllerProvider.php
Created July 14, 2013 16:33
Base Silex ControllerProvider.
<?php
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
class CustomControllerProvider implements ControllerProviderInterface
{
public function connect(Application $app)
{
@cybrown
cybrown / android-json-fix.js
Created August 8, 2013 12:33
Fix for old android JSON.parse.
if (window.JSON && window.JSON.parse) {
try {
JSON.parse(null);
} catch (e) {
var originalParse = JSON.parse;
JSON.parse = function(value) {
if (value) {
return originalParse(value);
} else {
return value;
@cybrown
cybrown / worker.js
Created August 28, 2013 15:24
Create a web worker from a function
var workerWithFunction = function (func) {
return new Worker(window.URL.createObjectURL(
new Blob(['(' + func.toString() + ')()'], {type: 'text/javascript'})
));
};
@cybrown
cybrown / inheritance.js
Created September 2, 2013 15:57
Javascript Prototype Inheritance
var Parent = function (arg1, arg2) {
// TODO Define instance members here...
};
Parent.prototype.doSomething = function (arg) {
// TODO Do something...
};
var Child = function (arg1, arg2) {
Parent.call(this, arg1, arg2) // Call to super constructor