Skip to content

Instantly share code, notes, and snippets.

View ismasan's full-sized avatar

Ismael Celis ismasan

View GitHub Profile
/* Bind one handler to many events
USAGE:
channel.bindMany('event1', 'event2', 'event3', function( data ){
// do something
})
-------------------------------------------------------*/
Pusher.Channel.prototype.bindMany = function(){
var handler = Array.prototype.pop.call(arguments);
for (var i = 0; i< arguments.length; i++){
this.bind( arguments[i], handler )
@ismasan
ismasan / async_require.js
Created October 25, 2010 15:59
Dynamically require scripts in the browser, with async callback for when all dependencies are loaded
/* Dynamically require scripts in the browser, with async callback for when all dependencies are loaded
Example:
function pageReady() {
// Init your code here
alert('All dependencies loaded!');
}
_require(['/json2.js', 'http://server.com/foo.js'], pageReady);
@ismasan
ismasan / purge.vcl
Created January 27, 2011 18:30
Varnish VCL example for purging wildcard URLs
# This goes in vcl_recv
# It gives you:
# curl -X PURGE http://some.example.com/.*
# curl -X PURGE http://some.example.com/blog/.*
# curl -X PURGE http://some.example.com/blog/2011/bar.html
# curl -X PURGE http://another.example.com/.*
#
if (req.request == "PURGE") {
# Wildcard, per-domain purging
purge("req.http.host == " req.http.host " && req.url ~ " req.url "$");
@ismasan
ismasan / lazy_member_iterator_with_async_loading.html
Created February 4, 2011 05:08
Load iterator with initial subset of data, async remote fetching (in batches) when iterating.
<html>
<head>
<title>JS Iterator</title>
<script>
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// NOTE: SKIP TO USAGE EXAMPLE AT THE BOTTOM FOR OVERVIEW
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function extend(one, two) {
for(var i in two){
one[i] = two[i]
@ismasan
ismasan / basic_fsm.js
Created February 8, 2011 01:39
Basic State Machine for Javascript objects
//++++++++++++++++++++++++++++++++++++++++++++++++
// Basic FSM implementation
//++++++++++++++++++++++++++++++++++++++++++++++++
var StateMachine = function(initial_state) {
var transitions = {},
state_callbacks = {enter:{}, exit:{}},
addTransition = function (from, to, guard) {
transitions[from] = transitions[from] || {};
transitions[from][to] = guard || function () {return true};
@ismasan
ismasan / cross_browser_columns.html
Created February 9, 2011 17:51
Columns that work even on IE
<!-- This is how you lay out cross-browser columns. Stick to this and you're golden -->
<style>
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
@ismasan
ismasan / router.js
Created May 9, 2011 14:09
JavaScript GET and event router. Only works in modern browsers.
/* Like Sammy.js but without the extra crap such as templating, and only for GETs and events
Features:
.get(path, handler)
Binds changes on the browser's location hash to a handler function, a la Sammy
.sub(event_name, handler)
Binds custom event name to event handler. Event names can have :tokens just like location hashes. Ie:
@ismasan
ismasan / thin_async.ru
Created May 14, 2011 16:29
Benchmark serving on-the-fly image resizes over HTPP, syn and async
# thin start -R thin_async.ru -p 3000
#
# ab -c 100 -n 500 http://127.0.0.1:3000/
#
# Concurrency Level: 100
# Time taken for tests: 8.888 seconds
# Complete requests: 500
# Failed requests: 0
# Write errors: 0
# Total transferred: 6243500 bytes
@ismasan
ismasan / phantom_js_url_cycle.js
Created May 15, 2011 15:24
Super easy web screen scraping with Phantom.js. Screenshots example.
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Cycle array of URLs and process with phantom.js (http://www.phantomjs.org/)
Adds Array.prototype.forEachWebPage() iterator.
EXAMPLE:
Save screenshots. Command line:
phantomjs phantom_js_url_cycle.js ./screenshots
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[
@ismasan
ismasan / js_model_scopes.js
Created May 19, 2011 15:56
Define collection 'scopes' for JsModel that emit 'add' and 'remove'
/* Bindable scopes for JsModel
Usage:
var User = Model('user', function () {
this.use(Model.Scopes)
});
// Define scopes
User.scope('with_i', function (model) {
return /^i/i.test(model.attr('name'))