Skip to content

Instantly share code, notes, and snippets.

View mauriciosoares's full-sized avatar
:shipit:
[object Object]

Mauricio Soares mauriciosoares

:shipit:
[object Object]
  • Automatiq
  • São Paulo
View GitHub Profile
@mauriciosoares
mauriciosoares / gist:9762678
Created March 25, 2014 14:12
Creating custom events with jQuery
// I'm not using any pattern here, for the sake of simplicity :)
// remember, it needs jquery
'use strict';
 
var app = {};
 
app.appView = function() {
  // gets new class and attatchs an custom event to it
  var newClass = new app.myNewClass();
function curry(fn) {
var slice = Array.prototype.slice,
stored_args = slice.call(arguments, 1);
return function() {
var new_args = slice.call(arguments),
args = stored_args.concat(new_args);
return fn.apply(null, args);
}
}
var App = App || {};
Module = function(ns_string) {
var parts = ns_string.split('.'),
parent = App,
i;
if(parts[0] == 'App') {
parts = parts.slice(1);
}
function Sandbox() {
var args = Array.prototype.slice.call(arguments),
// last arg is the callback
callback = args.pop(),
// gets all modules beeing passed as different args, or as an Array
modules = (args[0] && typeof args[0] === 'string') ? args : args[0],
i;
var Constr = (function() {
var id = 0;
function cl() {
this.id = id += 1;
}
cl.prototype.getId = function() {
return this.id;
var constant= (function() {
var allowed = {
string: true,
number: true,
boolean: true
},
ownProp = Object.prototype.hasOwnProperty,
constants = {},
prefix,
isDefined;
var device = navigator.userAgent.toLowerCase();
if(device.indexOf('mac') != -1) {
console.log('É um mac');
} else if(device.indexOf('iphone') != -1) {
console.log('É um iphone');
} else if(device.indexOf('android') != -1) {
console.log('É um iphone');
} else {
console.log('Não identificado');
// with this extend method, the second object is created, but there is no object reference between them.
var extend = function(parent, child) {
var i = 0,
child = child || {},
toStr = Object.prototype.toString,
arrayRef = '[object Array]';
for(i in parent) {
if(parent.hasOwnProperty(i)) {
// can be used for browsers that doesn't have "bind" implemented
if(typeof Function.prototype.bind === 'undefined') {
Function.prototype.bind = function(thisArgs) {
var slice = Array.prototype.slice,
args = slice.call(arguments),
fn = this;
return function() {
return fn.apply(thisArgs, args.concat(slice.call(arguments)));
var CarMaker = function() {};
CarMaker.prototype.drive = function() {
return 'My car has ' + this.wheels + ' wheels';
};
CarMaker.factory = function(type) {
var car;
if(typeof CarMaker[type] !== 'function') {