Skip to content

Instantly share code, notes, and snippets.

@jquense
jquense / SoundManager2
Last active August 29, 2015 13:55
Mock sm2 as a CommonJs module
(function(window, _undefined){
if ( window === _undefined )
throw new Error('SoundManager requries a window object!');
// here be the lib code
// use can attach what you must to the window object here!
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
//CommonJs
// the "strict" way is to never expose as a global object if possible, so no sm2 on the window
@jquense
jquense / gist:8981812
Created February 13, 2014 19:14
cast emitter as promise
Promise.castEmitter = function (item, resolveEvents, rejectEvents) {
var methods = {
on: _.has(item, 'on') ? 'on' : 'bind',
off: _.has(item, 'off') ? 'off' : _.has(item, 'removeListener') ? 'removeListener' : 'unbind'
};
function unbindAll(events, fn) {
for (var i = 0; i < events.length; i++)
@jquense
jquense / gist:9fc74f777f1ddd74ca2b
Created May 19, 2014 18:19
Aborting Bluebird Promise
var xhr = $.ajax({}).then(null, function(){})
var promise = Promise.cast(xhr)
xhr.abort()
request('http://ugenr.dk/', function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('span#ugenr').each(function(i, element){
var ugeNr = $(this).text();
ugeNr = ugeNr.split(" ");
ugeNr = ugeNr[1]
request('http://skema1.domain.com/services/elevSkema.ashx?idx=randomNumber&week='+ugeNr, secondCallback);
function getDay(callback) {
request('http://www.timeanddate.com/', function date(error, response, html){
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('span#ij1').each(function(i, element){
var dag = $(this).text();
console.log(dag);
@jquense
jquense / gist:6508bfce7a248f85434e
Created November 14, 2014 00:00
clone with props no extra 0.12
'use strict';
var React = require('react')
, hasOwn = Object.prototype.hasOwnProperty
, version = React.version.split('.').map(parseFloat)
, RESERVED = {
className: resolve(joinClasses),
children: function(){},
key: function(){},
ref: function(){},
style: resolve(extend)
@jquense
jquense / gist:44315ee5eb506a2aafe6
Last active August 29, 2015 14:10
Promise API conundrum
// my thing
Thing = {
addPromise: function(fn){
Promise.resolve(fn.call(this, 'input'))
.then(function(val){
//do wahtever
})
},
compose([your, code, looks, like, shit]);
function compose(_chain) {
chain = _chain.reverse();
(function next(index, args) {
args.push(callback)
if(index < chain.length)
chain[index].apply(this, args)
function(msg){
dispatcher.dispatch('CREATE_MSG', msg);
api.saveMsg(msg).then(msg => {
dispatcher.dispatch('CREATE_MSG_COMPLETE', msg) //id is available here
})
}
@jquense
jquense / Stores.js
Last active August 29, 2015 14:16
Fluxy errors
var loginStore = Store({
init(dispatcher){
var self = this;
dispatcher.register(function(action, payload){
if( action === 'LOGIN')
self.login()
})
},