Skip to content

Instantly share code, notes, and snippets.

Zhuochun zhuochun

Block or report user

Report or block zhuochun

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@zhuochun
zhuochun / app.commonjs.js
Created Jun 14, 2012 — forked from kwhinnery/app.commonjs.js
Ti.include, browser, and CommonJS module
View app.commonjs.js
//CommonJS style
var mod = require('browser.ti.module');
mod.sayHello('Kevin');
View Anonymous Closures
(function () {
// ... all vars and functions are in this scope only
// still maintains access to all globals
}());
@zhuochun
zhuochun / example-user.js
Created Jun 14, 2012 — forked from nijikokun/example-user.js
Beautiful Validation... Why have I never thought of this before?!
View example-user.js
var user = {
validateCredentials: function (username, password) {
return (
(!(username += '') || username === '') ? { error: "No Username Given.", field: 'name' }
: (!(username += '') || password === '') ? { error: "No Password Given.", field: 'pass' }
: (username.length < 3) ? { error: "Username is less than 3 Characters.", field: 'name' }
: (password.length < 4) ? { error: "Password is less than 4 Characters.", field: 'pass' }
: (!/^([a-z0-9_-]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' }
: false
);
@zhuochun
zhuochun / Singleton pattern
Created Jun 15, 2012
Singleton JavaScript Pattern
View Singleton pattern
// http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html
var Singleton = (function() {
var instance = null;
function PrivateConstructor() {
var rand = Math.round(Math.random() * 100);
this.getRand = function() {
return rand;
}
@zhuochun
zhuochun / core.test.js
Created Jul 12, 2012 — forked from drewwells/core.test.js
RequireJS and QUnit sitting in a tree
View core.test.js
//Wait for relevant code bits to load before starting any tests
define(['core.js'], function( core ) {
module("Core Tests");
test("Test core methods", function(){
expect(2);
equals( 1, 1, "A trivial test");
ok( true, "Another trivial test");
});
@zhuochun
zhuochun / pubsub.md
Created Jul 23, 2012 — forked from addyosmani/pubsub.md
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)
View pubsub.md

#Four Ways To Do Pub/Sub With jQuery 1.7 and jQuery UI (in the future)

Between jQuery 1.7 and some of work going into future versions of jQuery UI, there are a ton of hot new ways for you to get your publish/subscribe on. Here are just four of them, three of which are new.

(PS: If you're unfamiliar with pub/sub, read the guide to it that Julian Aubourg and I wrote here http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx)

##Option 1: Using jQuery 1.7's $.Callbacks() feature:

$.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, but for more information on it (including lots of examples), see my post on $.Callbacks() here:

@zhuochun
zhuochun / Convert to Number.js
Last active Oct 11, 2015
JavaScript Snippets from Everywhere
View Convert to Number.js
+1 === 1;
+0 === 0;
+undefined == NaN;
+null === 0;
+({1:2,2:3}).length == NaN
// Underscore -> https://github.com/documentcloud/underscore/blob/master/underscore.js#L80
@zhuochun
zhuochun / Backbone.js
Created Oct 22, 2012
Define JavaScript Frameworks
View Backbone.js
(function(){
// Initial Setup
// -------------
// Save a reference to the global object (`window` in the browser, `exports`
// on the server).
var root = this;
// Save the previous value of the `Backbone` variable, so that it can be
@zhuochun
zhuochun / Underscore.js
Created Oct 22, 2012
Extend Methods in JavaScript
View Underscore.js
/*
-> http://underscorejs.org/#extend
extend(destination, *sources)
Copy all of the properties in the source objects over to the destination object, and return the destination object.
It's in-order, so the last source will override properties of the same name in previous arguments.
*/
@zhuochun
zhuochun / js template.js
Created Oct 28, 2012
RequireJS + jQuery
View js template.js
/* ========================================
* <Project> - <Description>
*
* Author:
* Last Edit:
* ========================================
* <License>
* ======================================== */
define(function(require, exports) {
You can’t perform that action at this time.