Skip to content

Instantly share code, notes, and snippets.

View lmartins's full-sized avatar

Luis Martins lmartins

  • Goodwin Media, Multiweb
  • Portugal
View GitHub Profile
@lmartins
lmartins / gulpfile.js
Created February 1, 2014 14:56
My typical Gulpfile with Sass, CoffeeScript and Bower assets management
// Include gulp
var gulp = require('gulp'),
gutil = require('gulp-util'),
sass = require('gulp-sass'),
prefix = require('gulp-autoprefixer'),
coffee = require('gulp-coffee'),
coffeelint = require('gulp-coffeelint'),
concat = require('gulp-concat'),
plumber = require('gulp-plumber'),
changed = require('gulp-changed'),
gulp = require 'gulp'
gutil = require 'gulp-util'
sass = require 'gulp-sass'
prefix = require 'gulp-autoprefixer'
coffee = require 'gulp-coffee'
coffeelint = require 'gulp-coffeelint'
concat = require 'gulp-concat'
plumber = require 'gulp-plumber'
changed = require 'gulp-changed'
uglify = require 'gulp-uglify'
@lmartins
lmartins / pad.coffee
Created February 22, 2014 10:16
Add leading zeros to a number if applicable
pad = (num) ->
('0'+num).slice(-2)
console.log pad(8)
console.log pad(12)
@lmartins
lmartins / application.coffee
Created February 25, 2014 12:45
Load application javascript code based on the current view/controller http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
# http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
SITENAME =
common:
init: ->
# application-wide code
users:
init: ->
@lmartins
lmartins / Module.coffee
Created March 7, 2014 20:01
Module Pattern in CoffeeScript
Module = (->
_privateMethod = ->
console.log "Some text from the private method, called from the public method"
publicMethod = ->
console.log "something from the public method"
_privateMethod()
# métodos públicos que são expostos através do módulo
@lmartins
lmartins / protoObject.coffee
Last active August 29, 2015 13:57
Create Object passing it's prototype
# Creates a new object to be associated with the same prototype from the object
# passed in the constructor function
if typeof Object.create isnt 'function'
Object.create = (o) ->
F = ->
F:: = o
return new F()
newObject = Object.create(originalObject)
@lmartins
lmartins / SelfInvokingFunction.coffee
Last active August 29, 2015 13:57
Quick Self-invokinf functions in Coffeescript
#If you need to declare a self calling function in CoffeeScript, it's super easy, just do it:
message = "but stay safe"
do ->
message = "take chances"
alert message
alert message
# Outputs "takes chances" then "but stay safe"
# This gets super useful when you need to freeze a variable, like when in a for loop and using setTimeout, just pass in the variable:
@lmartins
lmartins / Closures.coffee
Created March 28, 2014 12:29
JS Closures in Coffeescript
# CLOSURES
showName = (firstName, lastName) ->
nameIntro = "Your name is "
makeFullName = ->
console.log nameIntro + firstName + " " + lastName
return makeFullName()
showName("Michael", "Jackson")
@lmartins
lmartins / ExtendObjects.coffee
Last active August 29, 2015 13:57
Extend Objects with CoffeeScript
extend = (target) ->
return unless arguments[1]
for property in arguments
sourceProp = property
for prop of sourceProp
if not target[prop] and sourceProp.hasOwnProperty(prop)
target[prop] = sourceProp[prop]
return
@lmartins
lmartins / Mixin.coffee
Last active August 29, 2015 13:57
Mixin Pattern in CoffeeScript
# MIXIN PATTERN IN COFFEESCRIPT
mixin = (target, source, methods...) ->
for method in methods
target[method] = source[method].bind(source)
return
# usage:
mixin toggle, toolbar.items[0], 'toggleActiveState'