Class JavascriptHelper
Javascript Helper class for easy use of JavaScript.
JavascriptHelper encloses all methods needed while working with JavaScript.
-
AppHelper
-
JavascriptHelper
Properties summary
-
string
-
array
Holds options passed to codeBlock(), saved for when block is dumped to output
-
boolean
Indicates whether all generated JavaScript should be cached for later output
-
boolean
Indicates whether generated events should be cached for later output (can be written at the
end of the page, in the
, or to an external file).
boolean
Indicates whether cached events should be written to an external file
array
Caches events written by event() for output at the end of page execution
array
Contains event rules attached with CSS selectors. Used with the event:Selectors JavaScript
library.
boolean
If true, automatically writes events to the end of a script or to an external JavaScript file
at the end of page execution
boolean
Indicates whether blocks should be written 'safely,' i.e. wrapped in CDATA blocks
array
HTML tags used by this helper.
boolean
Determines whether native JSON extension is used for encoding. Set by object constructor.
Method Detail
__construct( $options = array() )
Constructor. Checks for presence of native PHP JSON extension to use for object encoding
_utf8ToHex( $string )
Encode a string into JSON. Converts and escapes necessary characters.
afterRender( )
AfterRender callback. Writes any cached events to the view, or to a temp file.
Returns
null
blockEnd( )
Ends a block of cached JavaScript code
Returns
mixed
cacheEvents( boolean $file = false , boolean $all = false )
Cache JavaScript events created with event()
Parameters
-
boolean
$file
optional
false
- If true, code will be written to a file
-
boolean
$all
optional
false
- If true, all code written with JavascriptHelper will be sent to a file
Returns
null
codeBlock( string $script = null , array $options = array() )
Returns a JavaScript script tag.
Options:
- allowCache: boolean, designates whether this block is cacheable using the
current cache settings.
- safe: boolean, whether this block should be wrapped in CDATA tags. Defaults
to helper's object configuration.
- inline: whether the block should be printed inline, or written
to cached for later output (i.e. $scripts_for_layout).
Parameters
-
string
$script
optional
null
- The JavaScript to be wrapped in SCRIPT tags.
-
array
$options
optional
array()
- Set of options:
Returns
string
The full SCRIPT element, with the JavaScript inside it, or null,
if 'inline' is set to false.
escapeScript( string $script )
Escape carriage returns and single and double quotes for JavaScript segments.
Parameters
-
string
$script
- string that might have javascript elements
Returns
string
escaped string
escapeString( string $string )
Escape a string to be JavaScript friendly.
List of escaped ellements:
+ "\r\n" => '\n'
+ "\r" => '\n'
+ "\n" => '\n'
+ '"' => '\"'
+ "'" => "\'"
Parameters
-
string
$string
- $script String that needs to get escaped.
Returns
string
Escaped string.
event( string $object , string $event , string $observer = null , array $options = array() )
Attach an event to an element. Used with the Prototype library.
Parameters
-
string
$object
- Object to be observed
-
string
$event
- event to observe
-
string
$observer
optional
null
- function to call
-
array
$options
optional
array()
- Set options: useCapture, allowCache, safe
Returns
boolean
true on success
getCache( boolean $clear = true )
Gets (and clears) the current JavaScript event cache
Parameters
-
boolean
$clear
optional
true
Returns
string
includeScript( string $script = "" , array $options = array() )
Includes the Prototype Javascript library (and anything else) inside a single script tag.
Note: The recommended approach is to copy the contents of
javascripts into your application's
public/javascripts/ directory, and use @see javascriptIncludeTag() to
create remote script links.
Parameters
-
string
$script
optional
""
- Script file to include
-
array
$options
optional
array()
- Set options for codeBlock
Returns
string
script with all javascript in/javascripts folder
link( mixed $url , boolean $inline = true )
Returns a JavaScript include tag (SCRIPT element). If the filename is prefixed with "/",
the path will be relative to the base path of your application. Otherwise, the path will
be relative to your JavaScript path, usually webroot/js.
Parameters
-
mixed
$url
- String URL to JavaScript file, or an array of URLs.
-
boolean
$inline
optional
true
If true, the tag will be printed inline,
otherwise it will be printed in the
, using $scripts_for_layout
Returns
string
See
JS_URL
object( array $data = array() , array $options = array() , string $prefix = null , string $postfix = null , array $stringKeys = null , boolean $quoteKeys = null , string $q = null )
Generates a JavaScript object in JavaScript Object Notation (JSON)
from an array
Options
- block - Wraps the return value in a script tag if true. Default is false
- prefix - Prepends the string to the returned data. Default is ''
- postfix - Appends the string to the returned data. Default is ''
- stringKeys - A list of array keys to be treated as a string.
- quoteKeys - If false treats $stringKeys as a list of keys not to be quoted. Default is true.
- q - The type of quote to use. Default is '"'. This option only affects the keys, not the values.
Parameters
-
array
$data
optional
array()
- Data to be converted
-
array
$options
optional
array()
- Set of options: block, prefix, postfix, stringKeys, quoteKeys, q
-
string
$prefix
optional
null
- DEPRECATED, use $options['prefix'] instead. Prepends the string to the returned data
-
string
$postfix
optional
null
- DEPRECATED, use $options['postfix'] instead. Appends the string to the returned data
-
array
$stringKeys
optional
null
- DEPRECATED, use $options['stringKeys'] instead. A list of array keys to be treated as a string
-
boolean
$quoteKeys
optional
null
- DEPRECATED, use $options['quoteKeys'] instead. If false, treats $stringKey as a list of keys not to be quoted
-
string
$q
optional
null
- DEPRECATED, use $options['q'] instead. The type of quote to use
Returns
string
A JSON code block
value( mixed $val , boolean $quoteStrings = true )
Converts a PHP-native variable of any type to a JSON-equivalent representation
Parameters
-
mixed
$val
- A PHP variable to be converted to JSON
-
boolean
$quoteStrings
optional
true
- If false, leaves string values unquoted
Returns
string
a JavaScript-safe/JSON representation of $val
writeEvents( boolean $inline = true , array $options = array() )
Write cached JavaScript events
Parameters
-
boolean
$inline
optional
true
If true, returns JavaScript event code. Otherwise it is added to the
output of $scripts_for_layout in the layout.
-
array
$options
optional
array()
- Set options for codeBlock
Returns
string