joomla/joomla-cms — pull request #1260 by Fedik (2013-06-09 12:57:53+00:00)
Title:  [#28119] Add some standard for JavaScript initialisation + options storage

**This path:**
* adds possibility to use the one way for a scrips initialization
* the one container for store the scrip options
* it is the framework independent

**The one way for a scrip initialization:**

The scrip initialization works via the name spaced events. This is allow more interaction between a different extensions and their scripts.
For initialization need use just:
```javascript
Joomla.addEvent('domready', callback);
```
instead of:
```javascript
jQuery(document).ready(callback);
//or
window.addEvent('domready', callback);
```
And for tell for the other scripts about changes in DOM, need just:
```javascript
Joomla.fireEvent('load', 'changed-element')
```
The name spaced events allow more interaction.
Example we have:
```javascript
Joomla.addEvent('domready.extension1', callback1)
Joomla.addEvent('domready.extension1.gallery', callback2)
Joomla.addEvent('domready.extension1.slider', callback3)
Joomla.addEvent('domready.extension2', callback4)
```
At time "domready" event will be called all these callbacks.
if after some DOM manipulation we need re-init only the "extension1" and they children, then we just fire:
```javascript
Joomla.fireEvent('load.extension1', 'changed-element');
```
if need re-init only callback2 then just fire:
```javascript
Joomla.fireEvent('load.extension1.gallery', 'changed-element');
```
also can be passed more arguments if need:
```javascript
Joomla.fireEvent('load.extension1.gallery', 'changed-element', arg1, arg2 ...);
```
Here no limitation for the depth of the namespace.
Also there no limitation for used events, so can be used the "fake events" ...

**The one container for store the scrip options:**

In current state the developers add the script options as global variables, that can be not always safe, and not nice. This path allow to store options in the one global object, where the options separated by the extension name.
Example add the script option:
```php
$options = array(
'showAlert' => false,
'consolelog' => true,
'text' => 'Hi! I am "Text"!',
'someArray' => array('one', 'two', 'and so on')
);
$doc->setScriptOptions($options, 'plg_examplejs');
```
Access to the options in frontend:
```javascript
Joomla.optionsStorage.plg_examplejs;
```
Also this allow for the developers to move the all initialization code in to the separate file, and use the options storage for change the initialization logic.
The init file for plg_examplejs, example:
```javascript
Joomla.addEvent('domready', function(event, element){
var options = Joomla.optionsStorage.plg_examplejs;
//initialiszation code
});
```

**Some more thoughts:**
This path require the more additional changes in CMS, of course.
Example: load the core.js on top of the all scripts, change all Joomla behaviors.
Also would be good to put whole core.js in self executable function for prevent direct access to some Joomla variables.

Some more at the links.
**Links:**
* [[#28119] Add some standart for JavaScript initialisation, useful for AJAX](http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=28119) - feature request
* [One way JavaScript initialization](https://groups.google.com/d/msg/joomla-dev-platform/dWUbRsOAtNw/FJlROpBsbDYJ) - discussion in "Joomla! Platform Development"
* [One way JavaScript initialization (part 2)](https://groups.google.com/d/msg/joomla-dev-cms/jyKt5VE5PWw/sdFjnrwUlPgJ) - discussion in "Joomla! CMS Development"