Skip to content

Instantly share code, notes, and snippets.

View chris-tng's full-sized avatar
:octocat:

Chris (Tu) NGUYEN chris-tng

:octocat:
  • Vancouver, Canada
View GitHub Profile
// we will put in some functions of underscore
// define global object = root
var root = this;
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
# REG and replacement
# Strip out punctuation on predicates or bang methods since
# e.g. target?_without_feature is not a valid method name.
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
# this is parallel assignment
# $1 is arguments 1
@chris-tng
chris-tng / problem_add_checked_attribute_1.rb
Last active December 16, 2015 21:38
add_checked_attribute
# we want to develop a checked attribute to a class called add_checked_attribute
# like attr_accessor - it should generate a reader method and a writer method
# add_checked_attribute should be a Kernel method, unlike attr_accessor
# it will raise a Runtime exception if we assign the attribute to nil or false
# it file clearifies the requirement by test case
require 'test/unit'
class Person; end
@chris-tng
chris-tng / basics.rb
Last active December 16, 2015 16:40
learning ruby
# "many of ruby operators are implemented as methods" - p19 RPL (1)
# take example of << operator on Array, this operator allows to push an element into the array
arr1 = [1, 2, 3, 4] << 5
p arr1 # [1, 2, 3, 4, 5]
# one possible way to implement this operator as a method is
class Array
def <<(item)
self.push(item)
end
@chris-tng
chris-tng / my-auto-complete.html
Created April 24, 2013 03:38
a simple auto-complete example
<!DOCTYPE html>
<html>
<head>
<title>my auto-complete</title>
</head>
<style type="text/css">
.container {
padding-left: 20px;
}
@chris-tng
chris-tng / main.js
Last active December 16, 2015 07:59
extend jQuery
/*======= UTILITY =======*/
(function () {
wd.app.util = {};
// extend jquery
// clear the form: see http://www.learningjquery.com/2007/08/clearing-form-data
// only clear the visible state of the form
$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();

The dictionary refers to a Mediator as 'a neutral party who assists in negotiations and conflict resolution'.

In software engineering, a Mediator is a behavioural design pattern that allow us to expose a unified interface through which the different parts of a system may communicate. If it appears a system may have too many direct relationships between modules, it may be time to have a central point of control that modules communicate through instead. The Mediator promotes loose coupling by ensuring that instead of modules referring to each other explicitly, their interaction is handled through this central point.

If you would prefer an analogy, consider a typical airport traffic control system. A tower (Mediator) handles what planes (modules) can take off and land because all communications are done from the planes to the control tower, rather than from plane-to-plane. A centralized controller is key to the success of this system and that's really the role a mediator plays in software design.

In real-worl

The Mediator pattern enables communication (mediation) between views using a mediator object.In the latest version of Backbone, the Backbone object itself can be used as a mediator without the need of a seperate helper.

In the following example, triggerSomething in our ToolbarView uses the global event-bus on the Backbone object to broadcast an application wide event somethingHappened with data.

// First view
var ToolbarView = Backbone.View.extend({
  className: ".toolbar",
  events: {
    "click .button":   "triggerSomething"
@chris-tng
chris-tng / main.js
Last active December 16, 2015 07:49
main application uses requireJS
// Start the main app logic.
requirejs(['jquery', 'backbone', 'underscore', 'bootstrap'],
/*
* if we do not import bootstrap here, our modal does not work
* basically, bootstrap only extends some jQuery feature
* so does not import bootstrap meaning disable these features
*
* */
function ($, Backbone, _, bootstrap) {
@chris-tng
chris-tng / main.js
Last active December 16, 2015 07:48
requireJS configuration
/**
* basic requirejs configuration for
* jQuery
* underscore
* backbone
* bootstrap
*/
requirejs.config({
//By default load any module IDs from js/lib
baseUrl: 'js/lib',