Skip to content

Instantly share code, notes, and snippets.

View filippomangione's full-sized avatar

Filippo Mangione filippomangione

  • Boolean Careers
  • Milano
View GitHub Profile
@lastguest
lastguest / dehydrateFloat.js
Created February 14, 2014 19:13
JavaScript Fixed Float Dehydratation
// Assuming 5 fixed digits
function dehydrateFloat(f){
return Math.floor(f.toFixed(5)*100000).toString(36);
}
function hydrateFloat(dryf){
return (parseInt(dryf,36)/100000).toFixed(5);
}
@lastguest
lastguest / google_maps_block_panzoom.js
Created September 11, 2014 13:38
Google Maps : Block Pan&Zoom
// Init
google.maps.event.addDomListener(window, 'load', function() {
var map_container = document.getElementById("map-canvas");
var minZoomLevel = 2;
var startZoomLevel = 8;
var map = new google.maps.Map(map_container,{
center: new google.maps.LatLng(-34.397, 150.644),
@kopiro
kopiro / cookie-fast.html
Last active August 29, 2015 14:11
Cookie in a fast way
@kopiro
kopiro / youtube-nginx.conf
Created April 30, 2015 14:55
Youtube Thumb server with NGINX
server {
server_name youtube.flerovio.dev;
location @404 {
return 302 /sq$request_uri;
}
error_page 404 = @404;
location ~ /sq/(.+) {
// create pub-sub functionality
Backbone.pubSub = _.extend({}, Backbone.Events);
// view one needs to trigger an event in view2
View1 = Backbone.View.extend({
triggerView2Event : function() {
Backbone.pubSub.trigger('view2event', { 'some' : 'data' } );
})
@lastguest
lastguest / asyncLoadCSS.js
Created October 8, 2015 10:53
[JavaScript] asyncLoadCSS
function asyncLoadCSS (css_href) {
var css_link = function () {
var h = document.getElementsByTagName('head')[0]
var l = document.createElement('link')
l.rel = 'stylesheet'
l.href = css_href
h.parentNode.insertBefore(l, h)
}, RAF = requestAnimationFrame || mozRequestAnimationFrame
|| webkitRequestAnimationFrame || msRequestAnimationFrame
if (RAF) RAF(css_link); else window.addEventListener('load', css_link)
@lastguest
lastguest / JavascriptMiddlewares.js
Created September 20, 2013 15:50
Wrap a function with a middleware decorator.
// Generate a middleware
function middleware(mw){
return function(fn){
fn = (typeof fn == "function") ? fn : window[fn];
return function(){
var args = arguments || [];
if(false !== mw.apply(this,args)) fn.apply(this,args);
};
};
}
@fnakstad
fnakstad / gist:5383750
Created April 14, 2013 18:43
Attaches a function called "seed" to Mongoose models, which basically wraps the create function, but returns a promise. This way seeding can be done through a promise chain.
var mongoose = require('mongoose');
mongoose.Model.seed = function(entities) {
var promise = new mongoose.Promise;
this.create(entities, function(err) {
if(err) { promise.reject(err); }
else { promise.resolve(); }
});
return promise;
};
@dharFr
dharFr / jquery.observer.js
Created February 28, 2012 00:39
jQuery based observer pattern
;(function($) {
/*
* jQuery Observer pattern
* inspired by @addyosmani 's code
* see: http://addyosmani.com/resources/essentialjsdesignpatterns/book/#highlighter_506612
*/
var topics = [];
function getTopic(id) {
var callbacks;
topic = id && topics[id];
namespace.views.MyWizard = Backbone.Views.extend({
initialize: function() {
_.bindAll(this, 'render', 'wizardMethod');
}
render: function() {
this.wizardMethod();
return this;
},