Skip to content

Instantly share code, notes, and snippets.

View ivandotv's full-sized avatar
🏠
Working from home

Ivan Vlatković ivandotv

🏠
Working from home
View GitHub Profile
@ivandotv
ivandotv / machine.js
Created September 16, 2020 17:50
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@ivandotv
ivandotv / element_in_view.js
Created June 4, 2018 10:54
Detect when element is in view optionally using Intersection Observer
(function ($, window, document, undefined) {
var pluginName = "elementInView",
defaults = {
visibleWhenRatio: 0,
cb: $.noop,
outsideViewCallback: $.noop,
debounce: 100,
onlyOnce: true
};
@ivandotv
ivandotv / inheritsOLOO.js
Created February 11, 2015 23:06
inheritsOLOO
window.ikiThemes.Utils = (function ($) {
return {
//http://nodejs.org/docs/latest/api/util.html#util_util_inherits_constructor_superconstructor
inherits: function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype);
},
inheritsOLOO: function (superCtor) {
var o = Object.create(superCtor);
@ivandotv
ivandotv / gist:10023824
Last active August 29, 2015 13:58
Ladda bootstrap integration with Angular
angular.module('uiElements', []).directive('ikiLadda', [function () {
if (typeof Ladda === 'undefined') {
throw new Error('Ladda Library Missing');
// Ladda also depends on Spinner and it will throw error if that library is not present.
}
return {
@ivandotv
ivandotv / Implementation.js
Created November 18, 2013 18:00
Here's a template I sometimes use for OOP-similar behavior in JavaScript. As you can see, you can simulate private (both static and instance) members using closures. What new MyClass() will return is an object with only the properties assigned to the this object and in the prototype object of the "class." As you can see, the classes correctly in…
var bob = new MyClass();
bob.set_name('Bob');
bob.announce(); // id is 1, name shows as "Bob"
var john = new MyChildClass('Doe');
john.set_name('John');
john.announce(); // id is 2, name shows as "John Doe"
alert(john instanceof MyClass); // true
@ivandotv
ivandotv / Function Hoisting.js
Created September 2, 2013 15:43
As you know, all variables, no matter where in the function body they are declared, get hoisted to the top of the function behind the scenes. The same applies for functions because they are just objects assigned to variables. The only “gotcha” is that when using a function declaration, the definition of the function also gets hoisted, not only i…
/ antipattern
// for illustration only
// global functions
function foo() {
alert('global foo');
}
function bar() {
alert('global bar');
}
function hoistMe() {
// this is a function expression,
// pased as an argument to the function `callMe`
callMe(function () {
// I am an unnamed function expression
// also known as an anonymous function
});
Background | 59
// this is a named function expression
callMe(function me() {
// I am a named function expression
@ivandotv
ivandotv / Javascript memoization
Created August 8, 2013 13:12
Javascript memoization
var myFunc = function (param) {
if (!myFunc.cache[param]) {
var result = {};
// ... expensive operation ...
myFunc.cache[param] = result;
}
return myFunc.cache[param];
};
// cache storage
myFunc.cache = {}
@ivandotv
ivandotv / functions.php
Created July 8, 2013 20:30
Aktivacija plugina samo ako ga tema explicitno podrzava.
add_action('after_setup_theme','theme_setup');
function theme_setup()
{
add_theme_support('prefix-someCPT');
}