Skip to content

Instantly share code, notes, and snippets.

View juliocesar's full-sized avatar

Julio Cesar Ody juliocesar

View GitHub Profile
@juliocesar
juliocesar / for-loops.js
Created May 3, 2013 04:42
ES6 - for loops
// ES6 for loops
// =============
// Things in ES6 can be "iterable". Arrays are iterable by default.
var fruits = ['Apple', 'Banana', 'Grape'];
for (var fruit of fruits)
console.log('Fruit: ' + fruit);
@juliocesar
juliocesar / destructuring.js
Created May 3, 2013 04:31
ES6 - destructuring
// ES6 destructuring
// =================
// Assume a Person "class".
Person.prototype.giveTwoThings = function() {
return ['This', 'That']
}
// var [a, b] = p.giveTwoThings()
@juliocesar
juliocesar / params.js
Created May 3, 2013 04:20
ES6 - method parameter changes
// ES6 method parameter changes
// ============================
// Default argument values are possible in ES6.
var Person = function(name = 'John Doe') {
this.name = name;
};
// var p = new Person();
@juliocesar
juliocesar / classes.js
Last active September 30, 2023 03:01
ES6 - classes
// ES6 classes example
// ===================
// Currently, with prototypal inheritance:
var Person = function(name) {
this.name = name;
};
Person.prototype.talk = function(words) {
sync: ( method, collection, options ) ->
return Backbone.sync(method, collection, options) unless 'localStorage' in window
content = localStorage.getItem( @url() )
if content and not navigator.onLine
options.success JSON.parse content
else
return Backbone.sync( method, collection, options ).done ( response ) =>
localStorage.setItem( @url(), JSON.stringify( response ) )
module Rack
class EnforceValidEncoding
def initialize app
@app = app
end
def call env
full_path = (env.fetch('PATH_INFO', '') + env.fetch('QUERY_STRING', ''))
if full_path.valid_encoding? && Rack::Utils.unescape(full_path).valid_encoding?
@juliocesar
juliocesar / gist:4405111
Last active December 10, 2015 07:58
Rake task for embedding external (but local) scripts and stylesheets in an HTML page
# Assuming that the original file is named index.template.html, and the file that will
# be actually served is index.html
task :bundle do
require 'nokogiri'
root = File.dirname __FILE__
document = Nokogiri::HTML File.read(root + '/index.template.html')
@juliocesar
juliocesar / gist:4263855
Created December 12, 2012 00:49
Using Backbone.View with regular (read: not all rendered in JS) HTML pages.

The idea is you can still use Backbone.View to structure components in the screen. Run the code below with each page render.

In CoffeeScript:

$ ->
  for el in $('[data-view]')
    $el = $ el
    viewName = $el.data 'view'
    view = window[viewName]

new view(el: el) if view?

@juliocesar
juliocesar / gist:4108378
Created November 19, 2012 00:45
Backbone.Model in localStorage
# Storing a Backbone.Model in localStorage
# ========================================
#
# You can then use localStorage as a collection. For an usage example, see
# https://github.com/juliocesar/factory/blob/master/coffeescripts/factory.coffee#L149
# This largely Just Worked™ for my needs. YMMV!
class MyModel extends Backbone.Model
sync: (method, model, rest...) ->
# Simplest (?) console action parsing that doesn't use gems.
# The idea, in a nutshell:
#
# $ mycommand foo
#
# * Calls `foo` or a default method as per the ACTIONS constant (see below).
#
# $ mycommand foo bar
#
# * Calls `foo` with "bar" as a parameter.