Pub/sub pattern gems to simplify your models and avoid callbacks:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# inspired by twitter's RECESS | |
# http://twitter.github.io/recess/ | |
# usage: git diff | ruby ~/linter.rb | |
class Linter | |
def process | |
@result = {} | |
STDIN.each_line do |line| | |
@line = line[2..-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OpenStruct | |
def initialize(args) | |
args.each_pair do |k,v| | |
instance_variable_set "@#{k}", v | |
end | |
end | |
def method_missing(name, *args) | |
instance_variable_get "@#{name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* QUÉ ES ESTO | |
* Una manera muy simple de organizar los eventos de una página de tal modo que no están | |
* esparcidos por el código javascript. Inspirado en las vistas de backbone.js | |
* | |
* POR QUÉ | |
* De este modo los eventos que escucha tu página están declarados en un lugar evidente. | |
* | |
* CÓMO USARLO | |
* Crea una clase View que herede de AbstractView. Esta clase debe definir: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class @AbstractView | |
constructor: -> | |
console.log "I'm not meant to be instantitated" | |
render: -> | |
for event_selector of @events | |
event_selector_array = event_selector.split(' ') | |
event = event_selector_array.reverse().pop() | |
selector = event_selector_array.reverse().join(' ') | |
if selector == 'document' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Clase de ejemplo o referencia para usarse con AbstractView. | |
*/ | |
View = (function(_super) { | |
__extends(View, _super); | |
function View() { | |
return View.__super__.constructor.apply(this, arguments); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = { | |
map: function(hash, lambda) { | |
return Object.keys(hash).map(function(key) { | |
var value = hash[key]; | |
return lambda(value, key); | |
}); | |
}, | |
omit: function(hash, unwanted) { | |
var tmp = {}; |
RxJS and Bacon.js equivalences:
Bacon.js | RxJS | |
---|---|---|
Create stream for keyup events from an element | var inputText$ = $([data-js=my-input]).asEventStream('keyup').map('.target.value'); | var inputText$ = Rx.Observable.fromEvent($('[data-js=my-input]'), 'keyup').pluck('target', 'value') |
Create stream for keyup events from a live element | var inputText$ = $(document).asEventStream('keyup', '[data-js=my-input]').map('.target.value'); | var inputText$ = Rx.Observable.create(function(observer) {$(document).on('keyup', '[data-js=my-input]', function(e) {observer.onNext(e.target.value);});}); |
Subscribe to a stream | inputText$.onValue(function(props) {…}); | inputText$.subscribe(function(props) {…}); |
Create stream from promises | Bacon.fromPromise($.get(url, {text: text})); | Rx.Observable.fromPromise($.get(url, {text: text})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100.times do |i| | |
out = '' | |
out << 'fizz' if (i%3).zero? | |
out << 'buzz' if (i%5).zero? | |
out << i.to_s if out.empty? | |
puts out | |
end |
OlderNewer