Javascript Helper pt. 2

So I just read my last post about the javascript helper and I realized it makes little to no sense.
I was in a rush and just wanted to document the finding. Here’s a second try at explaining the js helper:

Using Javascript on your site.
1. Add the helper to the controller.

To use javascript in your cakephp site you need to first set the $helpers array in your controller to:
var $helpers = array('other_helper', 'javascript', 'other_helper');

You’ll probably be using other helpers already so just add the ‘javascript’ to the array.

2. Save your .js file under /app/webroot/js this is the conventional place to put all your js files and will make life easier for you if you use this directory.

3. Call your file from the template.

Since the call


needs to be in the header of your page and all of your view templates get inserted after the call of your layout template (/app/views/layouts/default.thtml) you’ll need to insert this line inside the section:

link(”myfile.js”);?>

If you only want to include this in one page or in a class of pages you can create another layout template and call that from your controller using the $layout variable or the
setLayout() function.

Iif you must use an externally located .js file you can use the function $javascript->linkOut($url);

4. Use your javascript.

Now instead of writing out function() $javascript->codeBlock("function();"); and that will insert the script tags for you.
As of yet I don’t have much experience with the javascript helper so here is a list of functions litterally copied and pasted out of the 1.1 Helper:Javascript code. I’ll write more as I use the helper more.

codeBlock
* Returns a JavaScript script tag.
*
* @param string $script The JavaScript to be wrapped in SCRIPT tags.
* @param boolean $allowCache Allows the script to be cached if non-event caching is active
* @return string The full SCRIPT element, with the JavaScript inside it.

escapeScript
* Escape carriage returns and single and double quotes for JavaScript segments.
*
* @param string $script string that might have javascript elements
* @return string escaped string

escapeString
* Escape a string to be JavaScript friendly.
*
* List of escaped ellements:
* + “\r\n” => ‘\n’
* + “\r” => ‘\n’
* + “\n” => ‘\n’
* + ‘”‘ => ‘\”‘
* + “‘” => “\\’”
*
* @param string $script String that needs to get escaped.
* @return string Escaped string.

event
* Attach an event to an element. Used with the Prototype library.
*
* @param string $object Object to be observed
* @param string $event event to observe
* @param string $observer function to call
* @param boolean $useCapture default true
* @return boolean true on success

cacheEvents
* Cache JavaScript events created with event()
*
* @param boolean $file If true, code will be written to a file
* @param boolean $all If true, all code written with JavascriptHelper will be sent to a file
* @return void

writeEvents
* Write cached JavaScript events
*
* @return string

object
* Generates a JavaScript object in JavaScript Object Notation (JSON)
* from an array
*
* @param array $data Data to be converted
* @param boolean $block Wraps return value in a