Pages

Tuesday, 4 November 2014

Removing an Event Listener when using Bind

Problem


If you want to keep the scope of your object in an event listener, you would use 'bind()' as the example below:

document.body.addEventListener('click',this.onBodyClick.bind(this));

The only problem with this is, when you want to remove the listener it doesn't recognise the function.
So this won't work:

document.body.removeEventListener('click',this.onBodyClick.bind(this));

Solution


Because 'bind()' creates a state of that function every time you call it, we need to store that state and use it to remove the listener.

1. Create the bind and store it as a handler variable:

var onBodyClickHandler= this.onBodyClick.bind(this);

2. Then add your listener with the handler variable:

document.body.addEventListener('click',onBodyClickHandler);

3. Now when you remove the listener use the handler variable:

document.body.removeEventListener('click',onBodyClickHandler);

Tuesday, 9 July 2013

Deployment Procedure (GitHub Projects)

Deployment Procedure (GitHub Projects)


This document outlines a recommended procedure to deploy projects into Dev, Staging and Live environments when using Git.


The idea is to control deployments and ensure code is deployed correctly and efficiently.


This recommendation starts from the project set up through to committing code and finally when code is pushed live.


Project Setup


1. Create the following branches:


  • DEV: The development branch.
  • STAGING: This branch should be used to push to the staging environment. Should only be merged with the ‘DEV’ branch.
  • MASTER: This will be the live branch. Should only be merged with the ‘STAGING’ branch.


2. Create the following folder structure for your project. Create the following folders in the root of the project:


  • src: This is for your code.
  • release notes: This is to keep track of releases/deployments (will be explained below).
  • deployment details: This will contain detailed information for each deployment/phase.


Deployment Details


For each phase/deployment create a text file which outlines the changes made to the project. Similar to a changelog.


Follow these simple rules:


  • naming the file: Name the file ‘phase’ followed by current phase number and then the date, such as ‘phase1_20130708’.  Remember to keep this in sync with the ‘release notes’


Release Notes


This folder will contain text files with all the file paths to any new or amended files.


For example:


\lib\com\fahimchowdhury\component\UIElement.js
\lib\com\fahimchowdhury\display\DisplayObject.js


Follow these simple rules:


  • naming the file: Name the file ‘release_phase’ followed by current phase number and then the date, such as ‘release_phase1_20130708’.  Remember to keep this in sync with the ‘deployment details’



Deployment Procedure


  1. Create a deployment details text file outlining what the next release contains and follow the correct naming conventions.
  2. Create a release note and follow the correct naming conventions.
  3. When committing code, copy all the file paths and place it into your release note.
  4. When pushing to staging add the following text in the commit message before you write anything else. Replace N with your phase number.

    ‘STAGING RELEASE PHASE N’

  5. Tag the Master branch. Use the following naming conventions (replace N with your phase number and then add the date and time): 

    Phase_N_YYYYMMDD_HHMM

  6. When pushing to the live site add the following text in the commit message before you write anything else. Replace N with your phase number.

    ‘RELEASE PHASE N’




    Alternative Approach with Tags


    The is another effect way of deploying with ‘tags’. You only need two branches:


    • DEV: The development branch.
    • MASTER: This will be the staging branch. Should only be merged with the ‘DEV’  branch.


    Deployment Procedure


    1. Create a deployment details text file outlining what the next release contains and follow the correct naming conventions.

    2. Create a release note and follow the correct naming conventions.

    3. When committing code, copy all the file paths and place it into your release note.

    4. When pushing to staging add the following text in the commit message before you write anything else. Replace N with your phase number.

      ‘STAGING RELEASE PHASE N’

    5. When pushing to the live site Tag the Master branch. Use the following naming conventions (replace N with your phase number and then add the date and time):

      Release_
      Phase_N_YYYYMMDD_HHMM

    6. Deploy the tagged code to the live site, pull the code and upload the relevant changes. follow the ‘release notes’.

Wednesday, 19 June 2013

White Paper - Apply Quantum Physics to Design Patterns


(Published by Fahim Chowdhury. 6th June 2013)



Abstract

This paper will go discuss the ways to architect projects using quantum physics theories.

The idea is to apply parts of quantum theories to the way we code to increase performance and efficiency.

I will combine my research in Schrödinger's cat & Quantum Superposition to devise a unique design pattern that will allow a single object to take shape of its observer and in the case of coding the observation is done through a view.

The benefits of such as design pattern is that is makes it possible to reuse a single object which can adapter to its views. This will help optimise performance and apply a better coding practices.




About me

My name is Fahim Chowdhury (MSc, BSc), I am a Computer Science Masters graduate who has been working in Advertising in the past 5 years building and architecting projects.

I began as a Flash Developer and now I work on HTML5/Javascript projects.

About Quantum Physics (Quantum Mechanics)

Quantum theory explains the laws of very small, discrete units of energy.
According to the Quantum theory it is impossible to know the exact location and velocity of an electron therefore it can exist in more than one location at the same time.

In this paper I will be focusing on a specific part of quantum physic, the quantum superposition and Schrödinger's cat thought experiment.

Schrödinger's cat & Quantum Superposition

This is was a thought experiment which talked about a cat being place in a box with a barrel of unstable gunpower. Outside the box the cat’s state was both ‘dead’ and ‘alive’ (superposition ) because until it the box was opened and the cat was observed the cat would stay in a superposition. After the cat has been observed the state of the cat was forced to either be ‘dead’ or ‘alive’.

Why Apply Quantum physics to Coding (Quantum Computing)

Quantum theory contains many features which can allow us to code more effectively. We can look at quantum computing as an example; computer memory is made of bits which are either set to on or off. Instead of having one state, quantum computers can have multiple states simultaneously which allows much faster computation.

I will try and apply the same principles to architecting web projects.

I’ve started to combine the state design pattern with the schrodinger’s cat experiment and the superposition to try and build the first quantum design pattern.

Let look at a traditional state pattern.

State Design Pattern

This design pattern is to encapsulate different behaviours for a given object. A change in the state will trigger/update a method or a variable.





As you can see the object is flat and cannot contain associated states for multiple contexts/observers.

The Quantum Pattern

The idea is that an object will exist in more than one view/context at the same time but depending on the observing view it will determine the objects states and values.

So how do we accomplish this?

Each view will have an identity and the quantum object will register each view it belongs to. When a view switches to another view, the current views identity is store and the quantum object gets notified of its current observer. The object will change its state and values to associate it to the current view. This will create a quantum entanglement.

When the quantum objects state or values change, the data will be stored in an array/object under the current views identity. This allows the object to exist in each view keeping the association. The object now has multiple quantums depending on the observer.




Javascript Code Example


Below is an object that I have created which allows you to register observers, update its data and change values and appear when the onQuantumChange function is called.

var QuantumButton = {
currentObserverId : null,
display : null,
observers : [],
registerObserver : function(id) {
if (!this.observers[id])
this.observers[id] = {
text : "",
state : "visible",
className : "",
x : 0,
y : 0
};
},
updateData : function(key, val) {

if (this.observers[this.currentObserverId] && this.observers[this.currentObserverId][key]!=undefined)
{
this.observers[this.currentObserverId][key] = val;
this.updateButton();
}
},
create : function() {
if (!this.display) {
this.display = document.createElement("div");
this.display.style.position = "absolute";
}
this.addChildToObserver();
},
addChildToObserver : function() {
if (document.getElementById(this.currentObserverId)) {
document.getElementById(this.currentObserverId).appendChild(this.display);
}
},
removeChildFromObserver : function(id) {
if(!this.currentObserverId)return;
if (document.getElementById(id)) {
document.getElementById(id).removeChild(this.display);
}
},
updateButton : function() {

this.display.innerHTML ="<p>" +this.observers[this.currentObserverId].text+"</p>";
this.display.style.left = this.observers[this.currentObserverId].x + "px";
this.display.style.top = this.observers[this.currentObserverId].y + "px";
this.display.className = this.observers[this.currentObserverId].className;
if (this.observers[this.currentObserverId].state == "hidden") {
this.display.style.display = "none";
} else {
this.display.style.display = "block";
}
},
onQuantumChange : function(id) {
this.removeChildFromObserver(this.currentObserverId);
this.currentObserverId = id;
this.updateButton();
this.addChildToObserver();
}
}




How to use the QuantumButton

First we need to register observers to the object. I’ve created three divs in the HTML and I will register them. Then I set the current observer, create the object and set the values for the object in the current observer.



QuantumButton.currentObserverId="view1";
QuantumButton.registerObserver("view1");
QuantumButton.registerObserver("view2");
QuantumButton.registerObserver("view3");
QuantumButton.create();
QuantumButton.updateData("className","button");
QuantumButton.updateData("text","button in view 1");
QuantumButton.onQuantumChange("view1");




I create a button in the HTML to show and hide each of the divs. And in that click function I will be notifying the object about the change in observer.



QuantumButton.onQuantumChange("view"+currentView);

I can now set new values to the object and it will be store for this observer only and once the observer changes again it will adapt the values to the associating observer.



QuantumButton.updateData("className","button2");
QuantumButton.updateData("x",100);
QuantumButton.updateData("y",100);
QuantumButton.updateData("text","button in view 2");




Benefits of this Method

Having an object which can be placed in more than one place makes it reusable and for it to be unique to a particular environment allows it to be adaptive.

When building large scale projects which require objects that are similar such as buttons across views, this will save overhead. You only have one button which contains only the relevant information it needs to be in a different view. Instead of having multiple buttons you now only have one which will improve performance.

Conclusion

I have the concept and the idea to improve the way we code but this is only my first attempt and its far from perfect.
Now its time to see the design pattern working in real life projects and analyse the impact it has.



Reference List


Wikipedia: Quantum mechanics. http://en.wikipedia.org/wiki/Quantum_mechanics. Updated 14 June 2013 at 22:00.

Oracle ThinkQuest: What is Quantum Physics?. http://library.thinkquest.org/3487/qp.html.
Wikipedia: State pattern. http://en.wikipedia.org/wiki/State_pattern. Updated 12 March 2013 at 08:19.