Skip to content

Instantly share code, notes, and snippets.

View coodoo's full-sized avatar

Jeremy Lu coodoo

View GitHub Profile
@coodoo
coodoo / webdev.md
Created November 29, 2012 16:37 — forked from dideler/bootstrapping.md
Bootstrapping
@coodoo
coodoo / Backbone-Collection-View
Created December 1, 2012 08:15
Backbone Collection View
/**
* Backbone Collection View
*
* A light-weight approach to display filetered data from collection by assigning rules, without changing original models,
* which works just like CollectionView in other languages (ie: Java, WPF and AS3).
*
* Usage:
*
* 1. Filter by single field:
* this.wineList.filterView( {Country:'France'}} );
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-requirejs');
grunt.initConfig({
requirejs: {
std: {
options: {
almond: true,
baseUrl: "lib",
@coodoo
coodoo / gist:821a77ac574acfb09a65
Last active October 21, 2015 02:01
Just picking up rx and building this as a practice, wondering what's the most elegant way to implement it?
- demo
- http://cl.ly/image/2s0W0z1X3g2q
- spec
- show tooltip after mouseover the button for 300ms
- if mouseout within 300ms, don't show tooltip
- hide tooltip when mouseout the button
- hide tooltip after 1000ms
@coodoo
coodoo / b
Last active December 10, 2015 16:08
What if we can assign an action (callback) for each of the 'eat' event ? this way switch/case would be eliminated.
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'eat', from: 'hungry', to: 'satisfied', action: 'foo' },
{ name: 'eat', from: 'satisfied', to: 'full', action: 'bar' },
{ name: 'eat', from: 'full', to: 'sick', action: 'coo' },
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry', action: 'doo' },
]});
@coodoo
coodoo / State_Machine.js
Created January 5, 2013 04:32
Implemented in a way that resembles SMC (http://smc.sourceforge.net/), primarily adding two callbacks a. action() b. guard() Changes I made are marked with "jx" in the comment.
(function (window) {
var StateMachine = {
//---------------------------------------------------------------------------
VERSION: "2.2.0",
//---------------------------------------------------------------------------
@coodoo
coodoo / randomColor.js
Created March 2, 2013 04:34
get random color in hex or rgba format.
/**
* @param useRGB Boolean, true for rgba format, false will return color in hex format
* @param randomAlpha Boolean, when using rgba format, decide whether to randomnize alpha value too
*/
function getRandomColor( useRGB, randomAlpha ){
if( useRGB ){
return "rgba("+
Math.floor(Math.random()*256)+","+
Math.floor(Math.random()*256)+","+
Math.floor(Math.random()*256)+","+
@coodoo
coodoo / gist:5251585
Created March 27, 2013 04:13
Weird behavior in chrome's dev tool console
I tried to pause the script, then enter following command in the console:
document.getElementById('foo')
The weird part is, sometimes it will show the object state, but some times it will just show the <div> tag, seems it's the result of toString() ?
Is there a way to always get object state in the console ?
Screen cap here: http://cl.ly/image/0V1a1p3t0e12
@coodoo
coodoo / getQueryString.js
Created May 25, 2013 08:06
input: "$filter=&$top=3&$skip=3&$orderby=firstName&$format=json&$inlinecount=allpages output: Object {$filter: "", $top: "3", $skip: "3", $orderby: "firstName", $format: "json"…} $callback: "callback" $filter: "" $format: "json" $inlinecount: "allpages" optional: enter key name as 2nd parameter to just get that value
$.extend({
getQueryString: function ( urlString, key) {
function parseParams() {
var params = {},
e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = urlString;
@coodoo
coodoo / arrow.js
Last active December 17, 2015 20:29
- Adjust bootstrap's popover arrow position to always be pointing as close as possible to the target (for example, a div or p element) - demo http://cl.ly/image/3Z02121G3G0P
var $foo = this.$('#foo').popover( o );
// 調整 popover 的 arrow 位置指向 label 中央
$foo.on('shown', function( evt ){
// 取得文字 label 的中間位置
var rect = $foo[0].getBoundingClientRect();
var targetPos = rect.left + rect.width/2;
// 找到 arrow 元件