Skip to content

Instantly share code, notes, and snippets.

package {
public function matches(source : *, ...values):void {
const len : uint = values.length;
// Walk through the supplied values and look for a match.
for (var i : uint = 0; i < len; i++) {
if (source === values[i]) {
return true;
}
}
@jonnyreeves
jonnyreeves / commonjs-wrapper.js
Created June 12, 2012 14:25
RequireJS Loading order
// This doesn't appear to be the case...
define(function (require) {
// Is there any guarantee that `moduleA` is loaded first?
var loadMeFirst = require('moduleA');
var loadMeSecond = require('moduleB');
});
@jonnyreeves
jonnyreeves / gist:2909133
Created June 11, 2012 08:47
JavaScript Object Factory Functions
// Wrap everything in an IIFE to avoid any globals leaking out.
(function (global) {
// Object Prototype, all instances created via 'matrix22' factory method will
// inherit from this object.
var matrix22Proto = {
zero: function() {
for (var i = 0; i < this.m.length; ++i) {
this.m[i] = 0.0;
}
@jonnyreeves
jonnyreeves / testrunner.html
Created June 2, 2012 13:32
Unit Testing Promises with Sinon.js
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<!-- when.js Promises implementation -->
<script src="https://raw.github.com/cujojs/when/master/when.js"></script>
<!-- Unit testing and mocking framework -->
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
@jonnyreeves
jonnyreeves / Logger.js
Created May 4, 2012 15:59
Quick and dirty JavaScript Logging Shim
/*global require, define */
define(function (require) {
"use strict";
var slice = Array.prototype.slice;
// Simple logging shim which allows log messages to be filtered and redirected to a logging solution
// of your choice when debugging.
var Logger = {
@jonnyreeves
jonnyreeves / index.html
Created April 23, 2012 21:38
JavaScript Class Structure using requireJS. The following code shows you how to create a Class definition in one JavaScript file and then import it for use in another; coming from an ActionScript 3 background this (and some of JavaScript specific traits)
<!DOCTYPE html>
<html>
<head>
<script data-main="usage" src="http://requirejs.org/docs/release/1.0.8/comments/require.js"></script>
</head>
<body>
<p>Check your JavaScript console for output!</p>
</body>
</head>
@jonnyreeves
jonnyreeves / gist:2043975
Created March 15, 2012 12:33
Factory Methods
public function create() : FacebookBaseAppRequestMarshaller
{
if (_appRequest is TargettedAppRequest)
{
return new FacebookTargettedAppRequestMarshaller(_appRequest as TargettedAppRequest, _payloadSerializer)
}
else if (_appRequest is FriendSelectorAppReqeust)
{
return new FacebookFriendSelectorAppRequestMarshaller(_appRequest as FriendSelectorAppReqeust, _payloadSerializer);
}
@jonnyreeves
jonnyreeves / DisplayObjectStreamLoaderImpl.as
Created March 6, 2012 19:59
Loads DisplayObjects from foreign domains
package uk.co.jonnyreeves
{
import flash.display.Loader;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
@jonnyreeves
jonnyreeves / NotificationMap.as
Created November 26, 2011 16:49
PureMVC RelaxedNotificationMap
package uk.co.jonnyreeves.puremvc
{
import flash.errors.IllegalOperationError;
import org.puremvc.as3.multicore.interfaces.INotification;
/**
* Maps a PureMVC Notification to a Function delegate. Add the mappings in your Mediator's Constructor and then
* delegate the calls to listNotificationInterests() and handleNotification() through to this class.
*
// I'm not a great fan of this as it violates the Law of Demeter.
facade.registerMediator(new BalanceDisplayMediator(appView.profileDisplay.balanceComponent));
facade.registerMediator(new ProfileDisplayMediator(appView.profileDisplay));
// Perhaps it's better to register the 'BalanceDisplayMediator' inside the 'ProfileDisplayMediator' instead? :S