Skip to content

Instantly share code, notes, and snippets.

@lukemorton
lukemorton / event-source.html
Created June 1, 2011 16:46 — forked from rwaldron/event-source.html
EventSource: The Glorified Long Polling Machine
Open your console.
<script src="event-source.js"></script>
@MaherSaif
MaherSaif / hook.js
Created April 22, 2012 12:49
simple javascript hooks system
Expected Result:
----------------
One
Two
Three
Four
Six
@kjantzer
kjantzer / PubSub.js
Created September 10, 2012 20:37
PubSub.js - Simple Publish/Subscribe "Class"
/*
Lightweight PUBLISH / SUBSCRIBE Class
Original code from: <http://blog.bobcravens.com/2011/01/loosely-coupled-javascript-using-pubsub/>
Can be initialized on other Objects to provide pub/sub functionality within that object
ex: this.events = new PubSub();
*/
@sivagao
sivagao / minimal_pubsub.js
Created April 2, 2013 10:02
minimal pubsub js
var pubsub = function(l, u, r, i) { // cool! 闭包并且初始化vars
return function(n, f) {
r = l[n] = l[n] || [], i = -1;
if (f && f.call) r.push(f);
else while (r[++i]) r[i].apply(u, arguments);
}
}({});
// subscribe to event
pubsub("eat_cookie", function() {
@joyrexus
joyrexus / README.md
Last active May 3, 2024 10:41 — forked from liamcurry/gist:2597326
Vanilla JS equivalents of jQuery methods

Sans jQuery

Events

// jQuery
$(document).ready(function() {
  // code
})
<?php
/**
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Data;
use Psr\Cache\CacheInterface;
@jhthorsen
jhthorsen / dom-events.js
Last active January 8, 2022 21:06
emit, on, once in pure javascript
Object.prototype.emit = function(name, d) {
this.dispatchEvent(new CustomEvent(name, { detail: d }));
return this;
};
Object.prototype.on = function(name, cb) {
this.addEventListener(name, cb, false);
return cb;
};
Object.prototype.once = function(name, cb) {
var wrapper = function() {
@alexis89x
alexis89x / broadcast-channel-es6.js
Last active February 21, 2024 16:56
Broadcast Channel API polyfill
/**
@class BroadcastChannel
A simple BroadcastChannel polyfill that works with all major browsers.
Please refer to the official MDN documentation of the Broadcast Channel API.
@see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API">Broadcast Channel API on MDN</a>
@author Alessandro Piana
@version 0.0.6
*/
/*
@learncodeacademy
learncodeacademy / pubsub.js
Created July 29, 2015 02:54
Basic Javascript PubSub Pattern
//events - a super-basic Javascript (publish subscribe) pattern
var events = {
events: {},
on: function (eventName, fn) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
},
off: function(eventName, fn) {
if (this.events[eventName]) {
/**
* Produces a function which uses template strings to do simple interpolation from objects.
*
* Usage:
* var makeMeKing = generateTemplateString('${name} is now the king of ${country}!');
*
* console.log(makeMeKing({ name: 'Bryan', country: 'Scotland'}));
* // Logs 'Bryan is now the king of Scotland!'
*/
var generateTemplateString = (function(){