Skip to content

Instantly share code, notes, and snippets.

View ValeriiVasin's full-sized avatar
🇺🇦

Valerii Vasin ValeriiVasin

🇺🇦
View GitHub Profile
@ValeriiVasin
ValeriiVasin / gist:1378015
Created November 18, 2011 22:49 — forked from oleksiilevzhynskyi/gist:1207023
ppa for sublime-text-2
sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text-2
@ValeriiVasin
ValeriiVasin / gist:1548808
Created January 2, 2012 00:35
[javascript patterns] Javascript inheritance.
var inherit = (function () {
var F = function () {};
return function (C, P) {
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
};
}());
@ValeriiVasin
ValeriiVasin / gist:1556360
Created January 3, 2012 19:04
[javascript patterns] Singleton.
// first realization: self-defining function
var Universe = function () {
var instance;
Universe = function () {
return instance;
};
Universe.prototype = this;
instance = new Universe();
instance.constructor = Universe;
// other functionality below...
@ValeriiVasin
ValeriiVasin / gist:1556603
Created January 3, 2012 19:54
[javascript patterns] Factory.
var CarMaker = function () {};
CarMaker.prototype.introduce = function () {
console.log("This is " + this.title);
};
CarMaker.factory = function (type) {
var constr = type,
newcar;
@ValeriiVasin
ValeriiVasin / gist:1556806
Created January 3, 2012 20:36
[javascript patterns] Decorator.
var Sale = function (price) {
this.price = price > 0 ? price : 100;
this.decorators_list = [];
};
Sale.decorators = {};
Sale.decorators.fedtax = {
getPrice: function (price) {
return price + price * 0.05;
@ValeriiVasin
ValeriiVasin / application.rb
Created January 14, 2012 17:35 — forked from aslam/application.rb
Rails 3 reset session using devise (actually not using devise)
class ApplicationController < ActionController::Base
...
before_filter :save_or_clear_session
def save_or_clear_session
if controller_name.eql?('sessions') and action_name.eql?('destroy')
request.reset_session
flash[:notice] = "Signed out successfully."
end
end
@ValeriiVasin
ValeriiVasin / gist:1617638
Created January 15, 2012 22:01
elegant IF statement
var type = 'hello';
// if (type === 'hello' || type === 'world' || type === 'maybe' || type === 'something') {
// alert('hi');
// }
if ( ({ hello: 1, world: 1, maybe: 1, something: 1 })[type] ) {
alert('hi');
}
@ValeriiVasin
ValeriiVasin / gist:1625039
Created January 17, 2012 05:45
[javascript patterns] Function binding
if ( !Function.prototype.bind ) {
Function.prototype.bind = function( obj ) {
var slice = Array.prototype.slice,
args = slice.call(arguments, 1),
self = this,
nop = function () {},
bound = function () {
return self.apply( this instanceof nop ? this : ( obj || {} ),
args.concat( slice.call(arguments) ) );
};
@ValeriiVasin
ValeriiVasin / pubsub.md
Created February 26, 2012 23:11 — forked from addyosmani/pubsub.md
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)

#Four Ways To Do Pub/Sub With jQuery 1.7 and jQuery UI (in the future)

Between jQuery 1.7 and some of work going into future versions of jQuery UI, there are a ton of hot new ways for you to get your publish/subscribe on. Here are just four of them, three of which are new.

(PS: If you're unfamiliar with pub/sub, read the guide to it that Julian Aubourg and I wrote here http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx)

##Option 1: Using jQuery 1.7's $.Callbacks() feature:

$.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, but for more information on it (including lots of examples), see my post on $.Callbacks() here:

@ValeriiVasin
ValeriiVasin / gist:2561868
Created April 30, 2012 19:30
Refresh CSS on the fly
javascript:(function(){var h,a,f;a=document.getElementsByTagName('link');for(h=0;h<a.length;h++){f=a[h];if(f.rel.toLowerCase().match(/stylesheet/)&&f.href){var g=f.href.replace(/(&|%5C?)forceReload=\d+/,'');f.href=g+(g.match(/\?/)?'&':'?')+'forceReload='+(new Date().valueOf())}}})()