Skip to content

Instantly share code, notes, and snippets.

View gryzzly's full-sized avatar

Misha Reyzlin gryzzly

View GitHub Profile
window.onload = function () {
// create canvas DOM element / object
var canvas = document.createElement("canvas");
document.body.appendChild(canvas);
// get context to draw on it
canvas = canvas.getContext("2d");
var drawSmiley = function (x, y) {
// "http://lorempixel.com/400/200/animals/3/" tiger
@gryzzly
gryzzly / JavaScript-custom-types-mechanics.js
Last active October 11, 2015 18:38 — forked from sym3tri/JavaScript-new-operator.js
How JavaScript OOP works
// constructor definition
var Foo = function() {
// when invoked with `new` it's as if the following implicitly happens
// this = { constructor: Foo };
// return this;
};
// For every function, `prototype` property is created automatically:
Foo.hasOwnProperty('prototype') // true
// request default HTML5 widget's code
$.getJSON(
'http://soundcloud.com/oembed' +
'?format=json' +
'&url=http://soundcloud.com/forss/flickermood'
).done(function (embedData) {
console.log(embedData.html);
});
// request Flash widget code
@gryzzly
gryzzly / karma-tyrtle.js
Created September 9, 2013 15:57
Karma Runner Tyrtle Adapter
/* global Tyrtle:true */
(function (window) {
/**
* Tyrtle renderer/reporter for Karma
* @param {Object} karma Karma runner instance
*/
function TyrtleKarmaRenderer(karma) {
this.karma = karma;
}
/* global Tyrtle */
// by convention, test files end with `-test`
var tests = Object.keys(window.__karma__.files).filter(RegExp.prototype.test.bind(/-test\.js$/));
window.requirejs.config({
// Karma serves files from '/base'
baseUrl: '/base/public/',
paths: {
'$': 'lib/dollar/dollar',
'backbone': 'vendor/backbone',
@gryzzly
gryzzly / tabTalk.js
Last active December 23, 2015 01:29
tabTalk
(function(root, mod) {
if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
mod(root.tabTalk || (root.tabTalk = {})); // Plain browser env
})(this, function(exports) {"use strict";
var
win = window
, storage = win.localStorage
, prefix = 'tab_'
, instance = prefix + date.now() // identify this tab
@gryzzly
gryzzly / logical OR vs array::some.md
Last active August 29, 2015 14:01
logical OR vs array::some
return !!(// double negation is required since logical operators dont always return Booleans
  ENV_VAR || 
  SomeLongStore.get('key') ||
  SomeConfig.get('layer', 'sub-layer') === 'required-value' ||
  this.anotherLongCondition()
)

vs

Example of a tedious task when using backbone MVC

You have three components each of which might have certain state. These components depend on one particular component that emits events. These three views have to all define the emitting component as a dependency and manually subscribe to the events in order to sync the state.

var View = require('lib/View'),
    source = require('source'); 
    
module.exports = View.extend({
@gryzzly
gryzzly / debounced-input.js
Last active December 15, 2016 21:51
Ember Debounced Input
//
// To be used like this:
//
//
// {{debounced-input
// placeholder="1000000"
// value=propertyName
// debounceWait=300 <-- debounce wait value
// fireAtStart=false <-- corresponds to Ember.run.debounce’s 4th param, if false, will run at the end of wait period
// class="form-control" <-- all regular text input attributes work