JavascriptHelper Class Info:

Class Declaration:

class JavascriptHelper extends AppHelper

File name:
cake/libs/view/helpers/javascript.php
Description:

Javascript Helper class for easy use of JavaScript.

JavascriptHelper encloses all methods needed while working with JavaScript.

Class Inheritance

AppHelper Helper Overloadable Object

Properties:

Show/Hide parent properties
  • action string

    Current action.

  • argSeparator string

    URL argument separator character

  • base string

    Base URL

  • _blockOptions array

    Holds options passed to codeBlock(), saved for when block is dumped to output

  • _cacheAll boolean

    Indicates whether all generated JavaScript should be cached for later output

  • _cachedEvents array

    Caches events written by event() for output at the end of page execution

  • _cacheEvents boolean

    Indicates whether generated events should be cached for later output (can be written at the end of the page, in the <head />, or to an external file).

  • _cacheToFile boolean

    Indicates whether cached events should be written to an external file

  • data array

    POST data for models

  • enabled boolean

    If true, automatically writes events to the end of a script or to an external JavaScript file at the end of page execution

  • helpers array

    List of helpers used by this helper

  • here string

    URL to current action.

  • namedArgs array

    List of named arguments

  • params array

    Parameter array.

  • plugin string

    Plugin path

  • _rules array

    Contains event rules attached with CSS selectors. Used with the event:Selectors JavaScript library.

  • safe boolean

    Indicates whether <script /> blocks should be written 'safely,' i.e. wrapped in CDATA blocks

  • tags array

    HTML tags used by this helper.

  • theme string

    The current theme name if any.

  • useNative boolean

    Determines whether native JSON extension is used for encoding. Set by object constructor.

  • validationErrors array

    Contains model validation errors of form post-backs

  • webroot string

    Webroot path

Method Summary:

Show/Hide parent methods

addClass

top

Adds the given class to the element options

Parameters:
  • array $options optional array ( )

    Array options/attributes to add a class to

  • string $class optional NULL

    The classname being added.

  • string $key optional 'class'

    the key to use for class.

Method defined in:
cake/libs/view/helper.php on line 774
Return

array Array of options with $key set.

Access

public

afterLayout

top

After layout callback. afterLayout is called after the layout has rendered.

Overridden in subclasses.

Method defined in:
cake/libs/view/helper.php on line 838
Return

void

Access

public

afterRender

top

AfterRender callback. Writes any cached events to the view, or to a temp file.

Method defined in:
cake/libs/view/helpers/javascript.php on line 715
Return

null

assetTimestamp

top

Adds a timestamp to a file based resource based on the value of Asset.timestamp in Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force' a timestamp will be added.

Parameters:
  • string $path required

    The file path to timestamp, the path must be inside WWW_ROOT

Method defined in:
cake/libs/view/helper.php on line 242
Return

string Path with a timestamp added, or not.

Access

public

beforeLayout

top

Before layout callback. beforeLayout is called before the layout is rendered.

Overridden in subclasses.

Method defined in:
cake/libs/view/helper.php on line 827
Return

void

Access

public

beforeRender

top

Before render callback. beforeRender is called before the view file is rendered.

Overridden in subclasses.

Method defined in:
cake/libs/view/helper.php on line 804
Return

void

Access

public

blockEnd

top

Ends a block of cached JavaScript code

Method defined in:
cake/libs/view/helpers/javascript.php on line 216
Return

mixed

cacheEvents

top

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

Method defined in:
cake/libs/view/helpers/javascript.php on line 484
Return

null

cakeError

top

Used to report user friendly errors. If there is a file app/error.php or app/app_error.php this file will be loaded error.php is the AppError class it should extend ErrorHandler class.

Parameters:
  • string $method required

    Method to be called in the error class (AppError or ErrorHandler classes)

  • array $messages optional array ( )

    Message that is to be displayed by the error class

Method defined in:
cake/libs/object.php on line 187
Return

error message

Access

public

call__

top

Parameters:
  • $method required

  • $params required

Method defined in:
cake/libs/view/helper.php on line 153

clean

top

Used to remove harmful tags from content. Removes a number of well known XSS attacks from content. However, is not guaranteed to remove all possiblities. Escaping content is the best way to prevent all possible attacks.

Parameters:
  • mixed $output required

    Either an array of strings to clean or a single string to clean.

Method defined in:
cake/libs/view/helper.php on line 278
Return

cleaned content for output

Access

public

codeBlock

top

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:

Method defined in:
cake/libs/view/helpers/javascript.php on line 179
Return

string The full SCRIPT element, with the JavaScript inside it, or null, if 'inline' is set to false.

__construct

top

Constructor. Checks for presence of native PHP JSON extension to use for object encoding

Parameters:
  • $options optional array ( )

Method defined in:
cake/libs/view/helpers/javascript.php on line 141
Access

public

dispatchMethod

top

Calls a method on this object with the given parameters. Provides an OO wrapper for call_user_func_array

Parameters:
  • string $method required

    Name of the method to call

  • array $params optional array ( )

    Parameter list to use when calling $method

Method defined in:
cake/libs/object.php on line 107
Return

mixed Returns the result of the method call

Access

public

domId

top

Generates a DOM ID for the selected element, if one is not set. Uses the current View::entity() settings to generate a CamelCased id attribute.

Parameters:
  • mixed $options optional NULL

    Either an array of html attributes to add $id into, or a string with a view entity path to get a domId for.

  • string $id optional 'id'

    The name of the 'id' attribute.

Method defined in:
cake/libs/view/helper.php on line 604
Return

mixed If $options was an array, an array will be returned with $id set. If a string was supplied, a string will be returned.

Todo

Refactor this method to not have as many input/output options.

escapeScript

top

Escape carriage returns and single and double quotes for JavaScript segments.

Parameters:
  • string $script required

    string that might have javascript elements

Method defined in:
cake/libs/view/helpers/javascript.php on line 293
Return

string escaped string

escapeString

top

Escape a string to be JavaScript friendly.

List of escaped ellements: + "\r\n" => '\n' + "\r" => '\n' + "\n" => '\n' + '"' => '\"' + "'" => "\\'"

Parameters:
  • $string required

Method defined in:
cake/libs/view/helpers/javascript.php on line 312
Return

string Escaped string.

event

top

Attach an event to an element. Used with the Prototype library.

Parameters:
  • string $object required

    Object to be observed

  • string $event required

    event to observe

  • string $observer optional NULL

    function to call

  • array $options optional array ( )

    Set options: useCapture, allowCache, safe

Method defined in:
cake/libs/view/helpers/javascript.php on line 423
Return

boolean true on success

field

top

Gets the currently-used model field of the rendering context.

Method defined in:
cake/libs/view/helper.php on line 570
Return

string

Access

public

get__

top

Default overload methods

Parameters:
  • $name required

Method defined in:
cake/libs/view/helper.php on line 151
Access

protected

getCache

top

Gets (and clears) the current JavaScript event cache

Parameters:
  • boolean $clear optional true

Method defined in:
cake/libs/view/helpers/javascript.php on line 496
Return

string

includeScript

top

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

Method defined in:
cake/libs/view/helpers/javascript.php on line 570
See

javascriptIncludeTag() to

Return

string script with all javascript in/javascripts folder

_initInputField

top

Sets the defaults for an input tag. Will set the name, value, and id attributes for an array of html attributes. Will also add a 'form-error' class if the field contains validation errors.

Parameters:
  • string $field required

    The field name to initialize.

  • array $options optional array ( )

    Array of options to use while initializing an input field.

Method defined in:
cake/libs/view/helper.php on line 751
Return

array Array options for the form input.

Access

protected

link

top

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 required

    String URL to JavaScript file, or an array of URLs.

  • boolean $inline optional true

    If true, the <script /> tag will be printed inline, otherwise it will be printed in the <head />, using $scripts_for_layout

Method defined in:
cake/libs/view/helpers/javascript.php on line 247
See

JS_URL

Return

string

loadConfig

top

Parses tag templates into $this->tags.

Parameters:
  • $name optional 'tags'

Method defined in:
cake/libs/view/helper.php on line 164
Return

array merged tags from config/$name.php

Access

public

log

top

Convience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.

Parameters:
  • string $msg required

    Log message

  • integer $type optional 2

    Error type constant. Defined in app/config/core.php.

Method defined in:
cake/libs/object.php on line 148
Return

boolean Success of log write

Access

public

model

top

Gets the currently-used model of the rendering context.

Method defined in:
cake/libs/view/helper.php on line 544
Return

string

Access

public

modelID

top

Gets the ID of the currently-used model of the rendering context.

Method defined in:
cake/libs/view/helper.php on line 559
Return

mixed

Access

public

_name

top

Gets the input field name for the current tag. Creates input name attributes using CakePHP's data[Model][field] formatting.

Parameters:
  • mixed $options optional array ( )

    If an array, should be an array of attributes that $key needs to be added to. If a string or null, will be used as the View entity.

  • string $field optional NULL

    If an array, should be an array of attributes that $key needs to be added to. If a string or null, will be used as the View entity.

  • string $key optional 'name'

    The name of the attribute to be set, defaults to 'name'

Method defined in:
cake/libs/view/helper.php on line 640
Return

mixed If an array was given for $options, an array with $key set will be returned. If a string was supplied a string will be returned.

Access

protected

Todo

Refactor this method to not have as many input/output options.

object

top

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

Method defined in:
cake/libs/view/helpers/javascript.php on line 603
Return

string A JSON code block

output

top

Returns a string generated by a helper method

This method can be overridden in subclasses to do generalized output post-processing

Parameters:
  • string $str required

    String to be output.

Method defined in:
cake/libs/view/helper.php on line 792
Return

string

Deprecated

This method will be removed in future versions.

overload

top

Overload implementation. No need for implementation in PHP5.

Method defined in:
cake/libs/overloadable_php5.php on line 36
Access

public

_parseAttributes

top

Returns a space-delimited string with items of the $options array. If a key of $options array happens to be one of:

  • 'compact'
  • 'checked'
  • 'declare'
  • 'readonly'
  • 'disabled'
  • 'selected'
  • 'defer'
  • 'ismap'
  • 'nohref'
  • 'noshade'
  • 'nowrap'
  • 'multiple'
  • 'noresize'

And its value is one of:

  • '1' (string)
  • 1 (integer)
  • true (boolean)
  • 'true' (string)

Then the value will be reset to be identical with key's name. If the value is not one of these 3, the parameter is not output.

'escape' is a special option in that it controls the conversion of attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.

If value for any option key is set to null or false, that option will be excluded from output.

Parameters:
  • array $options required

    Array of options.

  • array $exclude optional NULL

    Array of options to be excluded, the options here will not be part of the return.

  • string $insertBefore optional ' '

    String to be inserted before options.

  • string $insertAfter optional NULL

    String to be inserted after options.

Method defined in:
cake/libs/view/helper.php on line 334
Return

string Composed attributes.

Access

public

_persist

top

Checks for a persistent class file, if found file is opened and true returned If file is not found a file is created and false returned If used in other locations of the model you should choose a unique name for the persistent file There are many uses for this method, see manual for examples

Parameters:
  • string $name required

    name of the class to persist

  • $return required

  • string $object required

    the object to persist

  • $type optional NULL

Method defined in:
cake/libs/object.php on line 218
Return

boolean Success

Access

protected

Todo

add examples to manual

requestAction

top

Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.

Parameters:
  • mixed $url required

    String or array-based url.

  • array $extra optional array ( )

    if array includes the key "return" it sets the AutoRender to true.

Method defined in:
cake/libs/object.php on line 80
Return

mixed Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.

Access

public

_savePersistent

top

You should choose a unique name for the persistent file

There are many uses for this method, see manual for examples

Parameters:
  • string $name required

    name used for object to cache

  • object $object required

    the object to persist

Method defined in:
cake/libs/object.php on line 247
Return

boolean true on save, throws error if file can not be created

Access

protected

_set

top

Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.

Parameters:
  • array $properties optional array ( )

    An associative array containing properties and corresponding values.

Method defined in:
cake/libs/object.php on line 166
Return

void

Access

protected

set__

top

Parameters:
  • $name required

  • $value required

Method defined in:
cake/libs/view/helper.php on line 152

setEntity

top

Sets this helper's model and field properties to the dot-separated value-pair in $entity.

Parameters:
  • mixed $entity required

    A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"

  • boolean $setScope optional false

    Sets the view scope to the model specified in $tagValue

Method defined in:
cake/libs/view/helper.php on line 394
Return

void

Access

public

_stop

top

Stop execution of the current script. Wraps exit() making testing easier.

Parameters:
  • $status optional 0

Method defined in:
cake/libs/object.php on line 135
Return

void

Access

public

tagIsInvalid

top

Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors.

Parameters:
  • string $model optional NULL

    Model name as a string

  • string $field optional NULL

    Fieldname as a string

  • integer $modelID optional NULL

    Unique index identifying this record within the form

Method defined in:
cake/libs/view/helper.php on line 584
Return

boolean True on errors.

toString

top

Object-to-string conversion. Each class can override this method as necessary.

Method defined in:
cake/libs/object.php on line 64
Return

string The name of this class

Access

public

url

top

Finds URL for specified action.

Returns a URL pointing at the provided parameters.

Parameters:
  • mixed $url optional NULL

    Either a relative string url like /products/view/23 or an array of url parameters. Using an array for urls will allow you to leverage the reverse routing features of CakePHP.

  • boolean $full optional false

    If true, the full base URL will be prepended to the result

Method defined in:
cake/libs/view/helper.php on line 187
Return

string Full translated URL with base path.

Access

public

Link
http://book.cakephp.org/view/1448/url

_utf8ToHex

top

Encode a string into JSON. Converts and escapes necessary characters.

Parameters:
  • $string required

Method defined in:
cake/libs/view/helpers/javascript.php on line 324
Return

void

value

top

Converts a PHP-native variable of any type to a JSON-equivalent representation

Parameters:
  • mixed $val required

    A PHP variable to be converted to JSON

  • boolean $quoteStrings optional true

    If false, leaves string values unquoted

Method defined in:
cake/libs/view/helpers/javascript.php on line 683
Return

string a JavaScript-safe/JSON representation of $val

webroot

top

Checks if a file exists when theme is used, if no file is found default location is returned

Parameters:
  • string $file required

    The file to create a webroot path to.

Method defined in:
cake/libs/view/helper.php on line 198
Return

string Web accessible path to file.

Access

public

writeEvents

top

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

Method defined in:
cake/libs/view/helpers/javascript.php on line 527
Return

string