Skip to content

Instantly share code, notes, and snippets.

@francescoagati
francescoagati / alternate.js
Created July 17, 2011 23:20
example of jquery.declare
$.fn.declare.add({
'row.alternate': function(opt) {
this.find("tr.odd")
.css("background-color",opt.odd_color)
.end()
.find("tr.even")
.css("background-color",opt.even_color);
}
});
@francescoagati
francescoagati / example.coffee
Created July 17, 2011 01:57
jquery declarative named scope
$.fn.declare.add
'set.color': (color) -> @css({'color':color})
'set.bgcolor': (color) -> @css({'background-color':color})
$("div").declare('set.color','green').declare('set.bgcolor','red')
@francescoagati
francescoagati / wrap.coffee
Created July 6, 2011 22:47
wrapping a method of a prototype Object in javascript e coffeescript
class A
say_something: -> alert "hello"
class B extends A
#create new instance of B and call say_something
new B().say_something()
#wrapping method say_something of prototype A
@francescoagati
francescoagati / autoboxing.hx
Created July 5, 2011 21:04
Why haxe is a better php (2) - autoboxing, enumerations, macros
//support for autoboxing
// https://wiki.php.net/rfc/autoboxing
class Main {
public static function main() {
var hash = new Hash<Dynamic>();
hash.set("key1","value1");
@francescoagati
francescoagati / annotations.hx
Created July 5, 2011 20:42
Why haxe is a better php (1) - mixin - annotations - array deferencing - closure_and_chaining_function_call
//support for annotations
// https://wiki.php.net/rfc/annotations
@class_annotation({a:1,b:2})
class Main {
@range(1,100)
@francescoagati
francescoagati / pipe.coffee
Created July 5, 2011 12:12
jQuery:deferred an example of promise chained deferred with pipe and done. Coffeescript and javascript
promise= (fn) -> df= $.Deferred(fn).promise()
df=promise (df) -> setTimeout( (-> df.resolve(1000)), 1 )
df.pipe (x) ->
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 )
.pipe (x) ->
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 )
.pipe (x) ->
promise (df) -> setTimeout( (-> df.resolve(x+1000)), 2 )
.pipe (x) ->
@francescoagati
francescoagati / private_method.coffee
Created July 3, 2011 21:31
an example of private method in coffeescript with body class writable style ruby
class Animal
private_method= -> 1+1
@::sum= -> private_method()
dog=new Animal()
alert dog.sum()
class Fish extends Animal
delfin=new Fish()
@francescoagati
francescoagati / attr_accessor_with_closure.coffee
Created July 3, 2011 21:18
getter and setter with closure in coffeescript style attr_accessor of ruby
#this version don't work with prototype chain for generation variable prop_value with closure
Object::attr_accessor= (prop) ->
self=@
do ->
prop_value=null
self[prop]=(value) ->
prop_value=value if value?
prop_value
@francescoagati
francescoagati / attr_accessor.coffee
Created July 3, 2011 21:05
a simple getter and setter in coffeescript mimic attr_accessor of ruby (monkey patching version)
Object::attr_accessor= (prop) ->
self=@
self["_#{prop}"]=null
self[prop]=(value) ->
@["_#{prop}"]=value if value?
@["_#{prop}"]
Player={}
@francescoagati
francescoagati / async_map.coffee
Created July 1, 2011 15:37
test async.js map with jasmine
describe "async", ->
it "async not null", ->
expect(async).not.toBeNull()
it "respond to map", ->
expect(async.map).not.toBeNull()
it "[1,2,3] map (x) -> x + 1 eq [2,3,4]", ->
callback = jasmine.createSpy()
summer= (el,callback) -> callback null, el + 1
async.map([1,2,3],summer,callback)