Nasrul Hazim Bin Mohamad

[Kohana 3.3.0] Adding users using Auth Module

Jun
29

Nifty Modal Window Effects

Jun
25

TWITTER API MIGRATION FROM 1.0 to 1.1. (recent 06.2013) JAVASCRIPT JQUERY PHP

Jun
25

90 Great GIS Blogs to read (AFTER THIS ONE!!)

Jun
25

JavaScript – Overwrite alert()

Jun
15

Here I’m using pnotify to replace default alert

The Code

window.alert = function(title,message) {
				$.pnotify({
					title: title,
					text: message
				});
			};
alert('Say something','Wohoooooooooooo!'); // this should display alert in pnotify form.

Get Pines Notify Pines Notify

MySQL – Camel Case Function

Jun
11

Camel Case Function

DELIMITER $$

DROP FUNCTION IF EXISTS `pCase` $$
CREATE DEFINER=`root`@`localhost` FUNCTION `pCase`(str TEXT) RETURNS TEXT CHARSET latin1
    DETERMINISTIC
BEGIN
  DECLARE result TEXT DEFAULT '';
  DECLARE word TEXT DEFAULT '';
  DECLARE working_char TEXT DEFAULT '';
  DECLARE last_space INT DEFAULT 0;
  DECLARE word_boundry TEXT DEFAULT ' (){}[]-/'; #Used to decide when a new word begins.  Add you boundries here!!
  DECLARE ucase_words TEXT DEFAULT 'UK,USA,DVD,DVDs,GB'; #To make some words uppercase use ucase_words below.  The text entered here is used so dvds becomes DVDs.
  DECLARE i INT DEFAULT 1;  #Loop counter
  DECLARE found_boundry INT DEFAULT 1; #When we find a boundry set to 1 (True) else 0 (False)

  # handle NULL
  IF (str IS NULL) THEN
    RETURN NULL;
  END IF;

  # if 0 length string given
  IF (CHAR_LENGTH(str) = 0) THEN
    RETURN '';
  END IF;

  SET str = LOWER(str);

  # loop through each letter looking for a word boundry
  WHILE(i <= (LENGTH(str)+1)) DO

    #Set our working charater
    SET working_char=SUBSTRING(str, i-1, 1);

    #Find a word boundry
    IF(LOCATE(working_char, word_boundry)>0) THEN
      #Check if last word was in our uppercase list, using the example in the list to allow dvds to become DVDs
      IF(LOCATE(word, ucase_words)>0) THEN
        SET result=CONCAT(LEFT(result,(LENGTH(result)-LENGTH(word))),MID(ucase_words,LOCATE(word, ucase_words),LENGTH(word)));
      END IF;

      SET found_boundry=1;  #Set the boundry flag, then ignore
      SET result=CONCAT(result, working_char);
      SET word=''; #Reset word
    ELSE
      SET word=CONCAT(word, working_char);
      IF(found_boundry=1) THEN
        SET result = CONCAT(result, UPPER(working_char));  #After a boundry so upper case
        SET found_boundry=0;
      ELSE
        SET result = CONCAT(result, working_char);
      END IF;

    END IF;

    SET i=i+1;

  END WHILE;

  #Check if last word was in our uppercase list
  IF(LOCATE(word, ucase_words)>0) THEN
    SET result=CONCAT(LEFT(result,(LENGTH(result)-LENGTH(word))),MID(ucase_words,LOCATE(word, ucase_words),LENGTH(word)));
  END IF;

        RETURN result;

END $$

DELIMITER ;

Usage

SELECT pCase(`desired_column`) FROM `t`;

Reference: MySQL Bugs: #2430

How to write jQuery plugin with Public Methods

Jun
10

The Code

(function($) {

	var methods = {
		hi : function(who)
		{
			who = who != '' ? "HI " + who + "!": "Who's there?";
			alert(who);
		},
		hello : function(who)
		{
			who = who != '' ? "Hello " + who + "!": "Who's there?";
			alert(who);
		}
	};
	
    $.fn.Say = function(method) {
         // do some checking on method request, if exist call it & pass any arguments to the function
         // else return error
        if (methods[method])
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else if (typeof method === 'object' || !method)
		{
			return methods.init.apply(this, arguments);
		}
		else
		{
			$.error('Method ' + method + ' does not exist on jQuery.ipt');
		}
    };
	
})(jQuery);

How to use it

$(document).Say('hi','ahmad');
$(document).Say('hello','ahmad');

Reference: How to create a jQuery plugin with methods?

Kohana – Tips on Setting up Routes

Jun
04

Always set the default Route the last one, else specific Route will be overwritten

Set specific Route for a particular task

// specific Route for a particular task 
Route::set('doSomething', 'doSomething(/<controller>(/<action>(/<id>)))')
    ->defaults(
		array(
			'directory'  => 'doSomething',
			'controller' => 'page',
			'action'     => 'index',
		)
	);
	
// set default at the last position, else other routes will not work
Route::set('default', '(<controller>(/<action>(/<id>)))')
    ->defaults(
		array(
			'directory'  => '',
			'controller' => 'page',
			'action'     => 'index',
		)
	);

How to Create jQuery Plugins

Jun
02

Naming Convention

jquery.pluginName.js for non-minify version
jquery.pluginName.min.js for minified version
$.fn is an alias of the jQuery.prototype JavaScript property
To manipulate specific elements, use .each()

The Scaffolding

(function($) {
	// attach `Say` to jQuery plugin hook $.fn
	$.fn.Say = function(options) {
		
		// default options
		var default_option = {};
		
		// accepting options & extend the options
		var settings = $.extend( default_option, options );
		
		return this.each(function() {
			// Do something to each item
		});
	}
})(jQuery);

Do something with `Say` plugin

(function($) {
	// attach `Say` to jQuery plugin hook $.fn
	$.fn.Say = function(options) {
		
		// default options with key `what` and it's value `foo`
		var default_option = {
			what : 'foo'
		};
		
		// accepting options & extend the options
		var settings = $.extend( default_option, options );
		
		return this.each(function() {
			// set text to element
			if(settings.what)
			{
				// set text
				$(this).text( settings.what );
			}
			
		});
	}
})(jQuery);

Use it!

The Structure


|–index.html
|–/js
   |–/js/tuts.js
   |–/js/jquery.Say.js
   |–/js/jquery-1.8.3.min.js

The index.html
<!DOCTYPE html>
<html lang="en">
    <body>
		<button onclick="sayDefault()">Say Default</button>
		<button onclick="say()">Say Something</button>
		
		<p>hey</p>
		
		<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
		<script src="js/jquery.Say.js" type="text/javascript"></script>
		<script src="js/tuts.js" type="text/javascript"></script>
    </body>
</html>
The js/tuts.js
function say()
{
	$('p').Say({'what':'someting else'}); // overwrite default value
}

function sayDefault()
{
	$('p').Say();
}

Result

Default

Using Default Option

Overwritten options.what

Overwrite options