Skip to content

Instantly share code, notes, and snippets.

@jonnyreeves
jonnyreeves / gist:3169925
Created July 24, 2012 13:30
AS3 AutoMapper gist.
class PersonModel {
private var _firstName : String;
private var _lastName : String;
public function PersonModel(firstName : String, lastName : String) {
_firstName = firstName;
_lastName = lastName;
}
public function get firstName() : String {
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 / 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 / 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
@jonnyreeves
jonnyreeves / assertAfterSignal.as
Created September 22, 2011 16:05
AS3Signals FlexUnit 4 Async Assertion Helper
package uk.co.jonnyreeves.flexunit
{
import flash.events.EventDispatcher;
import mx.events.MetadataEvent;
import org.flexunit.async.Async;
import org.osflash.signals.ISignal;
public function assertAfterSignal(testCase : Object, signal : ISignal, handler : Function, timeout : int = 500) : void