Skip to content

Instantly share code, notes, and snippets.

View carlosvillu's full-sized avatar
🏠
Working from home

Carlos Villuendas Zambrana carlosvillu

🏠
Working from home
View GitHub Profile
@carlosvillu
carlosvillu / extend.js
Created November 15, 2012 10:44 — forked from tcorral/extend.js
Safe extend to bypass constructors that throw Errors.
/**
* This piece of code has been created to safely extend classes/objects in Javascript.
* After talk with a great friend that was upset trying to extend a class/object that throws an error
* if some parameter in the constructor of the class/object is missing.
*
* var Search = function ( sToSearch ) {
* if ( sToSearch == null ) { // Check if sToSearch is null or undefined
* throw new Error( 'Search needs a string to search' );
* }
* this.sToSearch = sToSearch;

These instructions work for the Raspberry Pi running Raspbian (hard float) and create a hardware optimized version of NodeJS for the Raspberry PI, (and include a working install and NPM!!!):

  1. Install Raspbian - http://www.raspberrypi.org/downloads

  2. Install the necessary dependecies:

sudo apt-get install git-core build-essential

(If you just installed git then you need to administer your git identity first, else adding the patches below will fail!!!)

@carlosvillu
carlosvillu / gist:3762253
Created September 21, 2012 15:44
REST API

Metodos HTTP

  • GET: No altera los datos en el servidor, solo puedes obtener datos de el
  • POST: Crea datos en el server
  • PUT: Altera datos en el server
  • DELETE: Lo borra de forma permanente

También puedes usar POST para hacer llamadas a métodos asíncronos, estos son aquellos que deben de realizar alguna operación que va a requerir más de un tiempo razonable de respuesta ( 100ms en el server ). Si usas POST no deberías de agregar variables GET en la URL.

Códigos de respuesta

@carlosvillu
carlosvillu / gist:3492897
Created August 27, 2012 22:24
Precompilo en el server templates Jade y los hago AMD compatibles
task 'build:client:jade', 'Compile Jade template to generate AMD complient modules', (file) ->
dir = "#{__dirname}/application/templates"
readdir dir, (err, files) ->
throw err if err
files.forEach (file, index) ->
console.log "application/templates/#{file}"
module = "define(function(require){var template = {{compiled}};return template;});"
template = readFileSync "#{dir}/#{file}", 'utf8'
compiled = module.replace /{{compiled}}/, jade.compile(template, client: true).toString()
writeFileSync "#{__dirname}/public/js/internal/templates/#{file.replace /.jade/, '.js'}", compiled, 'utf8'
@carlosvillu
carlosvillu / runner-mocha.coffee
Created August 25, 2012 17:36
Runner for phantom and mocha
system = require('system')
##
# Wait until the test condition is true or a timeout occurs. Useful for waiting
# on a server response or for a ui change (fadeIn, etc.) to occur.
#
# @param testFx javascript condition that evaluates to a boolean,
# it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
# as a callback function.
# @param onReady what to do when testFx condition is fulfilled,
@carlosvillu
carlosvillu / i18n.coffee
Created August 23, 2012 23:22
Idea para tener i18n con Require
class I18n
constructor: (@dicc={}) ->
_: (text) ->
@dicc[text] or text
browserLanguage = navigator.language ||
navigator.userLanguage ||
'en'
(function() {
var google, nock, req, request;
request = require('http').request;
nock = require('nock');
google = nock("www.google.com").get('/').reply(200, {
response: "ok!"
});
@carlosvillu
carlosvillu / gist:1451530
Created December 9, 2011 13:32
Lazy pattern para isMobile
var isMobile = function() {
var result = false;
for (var i = 0, len = SUPPORTED_OS.length; i < len && !result; i++) {
var mobile_os = SUPPORTED_OS[i];
$.os[mobile_os] && (result = true);
}
return isMobile = result;
};
@carlosvillu
carlosvillu / formas.js
Created October 10, 2011 17:15
Como crear clases y herencias en JS
var Forma = function(){};
Forma.prototype = {
getArea: function()
{
console.log('Get Area from Forma');
}
};
var Cuadrado = function()
{
@carlosvillu
carlosvillu / main.js
Created October 1, 2011 00:02
Uso de la función call
jQuery(function(){
jQuery('input[type=submit]').click(function(evt){
evt.preventDefault();
var string = jQuery('input[type=text]').val();
var simpleParser = {},
advancedParser = {},
wonderfulParser = {};
PARSER.call(simpleParser);