Skip to main content

Posts

Showing posts from 2012

JavaScript The Good Parts - To see the real beauty of the language

Recently, I finished reading the book "JavaScript The Good Parts" written by Douglas Crockford. Actually, many developers, although they have used JavaScript for many years, they haven't tried to learn it in a proper way. Therefore there is a great amount of poorly written codes in the world wide web. And also JavaScript said to be the world's most misunderstood programming language. So, this is a great book to read for any one who wants to see the real beauty of JavaScript.
The book has ten chapters and five appendixes which describes the good and bad parts of the language, and how to manipulate the good parts, and how to avoid the bad parts. I did read the book from the first page to the last page and found there is nothing to ignore or skip without reading. And my passion about JavaScript was increased exponentially because of this book, since I found there are many workarounds to overcome the limitations of the language.

After all, in any language the…

Javascript Module Pattern

Module pattern is one of the well know design patterns in JavaScript. I did read about this pattern, for the first time, in a publication done by Douglas Crockford. Since then, I have used this pattern in many occasions in my coding.

Using a module, an interface can be presented, by hiding the implementation and the state of an object. And it's a way of having private attributes and methods. A simple implementation is given below.


var containers = (function () {

        var module = {};

        var default_container_width = 500; //private property

        var possible_containers_list = new Array();
        possible_containers_list[1]="comments";
        possible_containers_list[2]="respond";   

        module.list = possible_containers_list; //public property

        function getContainerWidth() {//private function
            // ...
        }

        module.hideAll = function () {//public function
           // ...
        };

        return module ;
}());


To…

De Morgan's Laws in Programming

Recently, while I was reviewing some codes, I saw there were some conditional statements that check for the same condition but written in different ways. Most of these statements were written with common sense without using any mathematical analysis, since those are too simple to go for a more formal approach. The two identical conditional statements that has been written in different ways are given below.

01)

if ($comment['deleted'] == '1' || $comment['approved'] == '0') {
                unset($conversationsArray[$key]);
} else {
               ++$count;
}

02)

if ($comment['deleted'] == '0' && $comment['approved'] == '1') {
               ++$count;             
} else {
             unset($conversationsArray[$key]);
}

Obviously, the above lines say that the inverse of the first condition is equals to the second condition and vice versa. That is...

 ($comment['deleted'] == '1' || $comment['ap…

How to work around the limitations set by Same Origin Policy...

Last week, I wanted to make some Ajax requests from a page generated by some web application to another web application. These two applications are hosted in two different servers, with different domains. To do this type of a thing, you can't use a normal method because of the limitations set in place by "Same Origin Policy".

Same Origin Policy is a security concept for client side programing languages. This policy prevents the client side scripts accessing the pages from different web sites. For an example, if your application is hosted in the "hostA" and a client side script of your application wants to send an Ajax request to a another application that is hosted in "hostB", then this request will be prevented by the Same Origin Policy. To be able to make that type of a request, both host and protocol should be same, according to the Same Origin Policy.

For more details :
http://en.wikipedia.org/wiki/Same_origin_policy
https://developer.mozilla.org/En…

Ground Monitor

This is a species of monitor lizards that lives in Sri Lanka. In Sinhalese this animal is called "Talagoya". The one in these photos used to walk around the areas near my house in Sri Lanka, so one day it was captured to my camera.

How to avoid those little issues one can get when creating new projects with Zend Framwork...

CSS styles my not apply to the templates :
Some times this happens when you haven't created a virtual host for the application. After the virtual host is created styles will apply properly, or just use some url helper like "baseUrl" when linking the style sheets to the templates.

Error messages when using the commands like "zf enable layout", "zf create db-table ....", "zf create model ...", "zf configure db-adapter", and "zf create action ...." etc...
You may get error like "Action 'enable' is not a valid action", "Action 'configure' is not a valid action"......... etc...
Reason for this may be, the Zend library is not in the include path. If so add the library path to the include path. Or check whether there are old libraries in any directories of include paths that conflicts with the new one. Check whether there is a one as a PEAR package.

And check whether the "zf" command …

Time goes by so fast...

I haven't written anything in my blog for a long time, actually, this was completely forgotten through out the last one and half years, although there are many things to write about. This year, I am hoping to write some more things, at least the blog will be used as a my own reference to keep the things documented.

Last year, one of my best friends got a really nice idea, that is to document each an every little programing "how to"s for later reference in a blog. Now, that site has grown, and it's a nice place to find those day to day tech bits. - http://techbitsdaily.blogspot.com/

So, I am also going to add a new category called "howto" for keeping my day to day stuff. And this will be useful for keeping little things documented, to reduce the time wast of re-searching.