jQuery, CakePHP: Submit a form using ajax; implementing "quick save"

In CakePHP, If you want to submit a FORM using an AJAX request, use jQuery Form Plugin.

You can find a detailed example at http://marcgrabanski.com/article/cakephp-ajax-quick-save-jquery

An alternative to this plugin is invoking a javascript function, when a submit button is clicked, and which would read all the input data inside the form and send it to the controller.

Tcl/Tk: How to find out the Tcl version

How to find out the Tcl version

[yourLoginPrompt]$ tclsh
% info patchlevel
8.4.13
% puts $tcl_version
8.4
% info tclversion
8.4
%

Javascript: Make indexOf function working in IE

The indexOf function is a recent addition to the ECMA-262 standard and newly introduced in JavaScript 1.6, hence it may not work in some browsers. You can work around this by inserting the following piece of code at the beginning of your scripts.

Note: This function will work in FF1.5 or above, because this algorithm is exactly the one used in Firefox and SpiderMonkey(is the code-name for the Mozilla's C implementation of JavaScript).

if(!Array.indexOf) {
  Array.prototype.indexOf = function(obj) {
    for(var i = 0; i < this.length; i++) {
      if(this[i] == obj) { return i; }
    }

    return -1;
  }
}

Javascript: Easy Javascript Debugging using Firebug Console.

Instead of using the traditional 'alert' statements for debugging, the best way to debug is using Firebug console.
What you need : Firefox browser and Firebug addon

A simple example:
INSTEAD OF :

function foo() {
 if(some condition) {
    alert("Status update");
  }
}

USE :

function foo() {
 if(some condition) {
  console.log("Status update");
 }
}

For more details : http://getfirebug.com/logging

cakePHP: Passing multiple parameters from view to controller in cakePHP

There is no direct specification for passing multiple parameters from view (.ctp file) to controller (.php file) in cakePHP. It is intuitive !
The format of the url with a single parameter look like : ../myApp/myController/myFunction/parameter1

To pass multiple parameters simply append at the end which might look like
../myApp/myController/myFunction/parameter1/parameter2/parameter3

Some generic ways are:
1.
array ('action' = '/myFunction/'. $id .'/'.$another_id); 

2.
$Html-> link ('edit', array ('url'='/ myController/myFunction? Id ='. $Id. '& another_id ='. $another_id)). 


3.
$Html-> link ('edit', array ('action' = 'myFunction', $id,$another_id)). 


The function in myController would look like:
 function myFunction ($id, $another_id){
   // do something
}

Date formatting in JavaScript

We would be using 3 different methods to get the current system date, month and year.
  • getDate() : Returns the date
  • getMonth(): Returns the month
  • getFullYear(): Returns the year
#1: month/date/year (5/12/2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

document.write(sysMonth + "/" + sysDate + "/" + sysYear);

#2: date-month-year (12-May-2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

var monthNames = new Array("January", "February", "March",
               "April", "May", "June", "July", "August", 
               "September", "October", "November", 
               "December");

document.write(sysDate + "-" + monthNames[sysMonth] + "-" + sysYear);

#3: date-month-year (12th May 2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

var monthNames = new Array("January", "February", "March",
               "April", "May", "June", "July", "August", 
               "September", "October", "November", 
               "December");

var sup = "";

if(sysDate == 1 || sysDate == 21 || sysDate == 31) sup ="st";
else if (sysDate == 2 || sysDate == 22) sup = "nd";
else if (sysDate == 3 || sysDate == 23) sup = "rd";
else sup = "th";

document.write(sysDate + "" + sup + " " + monthNames[sysMonth] + " " + sysYear);

jQuery: Using jQuery with Other Libraries

Ever faced a problem to use jQuery with other Library ?
By default, jQuery uses "$" as a shortcut for "jQuery"

jQuery provides the convenience by defining our own shortcut which eliminates the conflict.
A simple example

<script src="jquery.js"> </script>
<script src="prototype.js"> </script>

<script type="text/javascript" >
jQuery.noConflict()  //now by default, $ is jQuery.
  //But you can reassign it to your own variable.
    var $jq = jQuery;
    //start using $jq for jquery's $
    $jq(document).ready(function() {
    $jq("div").hide();    });
</script>

Open a File Browser From Your Current Command Prompt/Terminal Directory


Open Finder in Mac OS X, the terminal type
open .

Open a File Browser in Windows, in the command prompt type:
explorer .
OR
start .

Open a File Browser in Linux, in the terminal type
nautilus .
OR
gnome-open .


PHP: Comparing dates/date strings in PHP

Comparing Date strings in PHP is easy using strtotime function.
The strtotime function returns # of seconds since Jan 1,1970 UTC.

Simple example:
$strtDate = strtotime("2009-10-3");
$endDate = strtotime("2010/11/9");
if($strtDate > $endDate){
 // do something
}
if($strtDate < $endDate){
 /do something
}

CSS/HTML : ID or CLASS attribute? Which one to use when ?

A simple, yet powerful classification of the DOM elements can done using the element attributes ID and CLASS. By giving a CLASS attribute, we can refer/select MULTIPLE elements with the same class. Example:

<div class="myclassA">first content</div>
<div class="myclassA">second content</div>
<div class="myclassA">third content</div>

Once we have such layout, we can access all the div's easily, some of them would be: 1. In the .CSS file as :

.myclassA{
 color : red;
}

2. If ur using JQuery:

$(".myclassA").css("color", "blue");

By giving a ID attribute, we can refer/select EXACTLY one element with that ID. Example:

<div id="myDivIdA">first content&lt;/div>
<div id="myDivIdB">second content</div>

Once we have such layout, we can access EXACTLY one div easily, some of them would be: 1. In the .CSS file as :

#myDivIdA{
 color : red;
}

2. If ur using JQuery:

$("#myDivIdB").css("color", "blue");

NOTE: 
When accessing, CLASS attributes are prefixed with '.'
and ID attributes are prefixed with '#'

Elements can have both CLASS and ID attributes. So you can can even more flexibility.

jQuery: $("#elementId") != document.getElementById("elementId")

The most natural assumption is that  $ in jQuery is equivalent to document.getElementById. But it is NOT !! Took me a while to figure it out.

$("#elementId")!=document.getElementById("elementId")

$("#elementId") method gives a jQuery object that is always an array of Elements.

So to the get the real DOM element you have to use $("#myElementId")[0] or the more readable $("#myElementId").get(0)

Gmail Adds Drag-and-Drop Attachment Uploads and Attach Calendar Invitation

A very cool feature from google GMAIL !
Now you can drag and drop attachments and insert invitations directly in 'compose mail'.
Simply drag single/multiple files into the 'Compose Mail' window !

ShiftIt: Windows 7 Aero Snap for Your Mac !

Wondered if you wanted to get the smart resizing introduced in Windows 7 on your Mac ?

Well Try this http://code.google.com/p/shiftit/
ShiftIt is a free download for Mac OS X only.

Theres another software Called Cinch http://www.irradiatedsoftware.com/cinch/ ,but you need to pay for it !

Easy and smart way to install Eclipse plugins

You can install the eclipse plugin in 2 standard ways:
1. Download the plugin from web and place both the "features" and "plugins" folder's contents into your eclipse installation features and plugins folders respectively.
2. You can install through "Update Manager"() by giving the update URL.

Note: The disadvantage with the above approaches are, all the installed plugins will work for only your eclipse installation. If your eclipse crashes, you will ended up with installing all the plugins that you did. And moreover every team member has to do the same every time they install Eclipse.

There is one other way that you can install Eclipse plugin and get rid of above disadvantage. And its pretty easy I believe. Please follow the steps below:

1. Create a folder structure like below anywhere in your shared folder. (The folder name must be 'eclipse' with subfolders matching exactly 'features' and 'plugins')







2. Download the plugins from web and place features folder contents and plugin folder contents into respective folders that you have created above.

3. Create a new folder called "links" in your eclipse home installation like below and create a new plain text file called plugins.link (Technically, it can be called anything.link). In it put the line path=...\My Shared Folder (i.e. path value should be the eclipse folder path that you have created above.)



4. Finally launch the eclipse. It will notices the links/plugin.link file and load any plugins from that path as well.

Advantage: With this approach you will be decoupling external plugins with your eclipse installation so that way you can have your team share the same plugins every time they change their project preferences or eclipse installation.




Click a button using Javascript

Here is how you click a button in using javascript.


<script type="text/javascript" >

  document.getElementById("mybutton").click;

</script>


This specifically helps when ur using AJAX helpers in cakePHP and you want to submit the AJAX form using javascript. In this case u will not be able to use either .submit or .onClick

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...