User tests: Successful: Unsuccessful:
Wiki: Adding the options to your JavaScript code
Example add the script option:
$options = array(
'showAlert' => false,
'consolelog' => true,
'text' => 'Hi! I am "Text"!',
'someArray' => array('one', 'two', 'and so on')
);
$doc->addScriptOptions('plg_examplejs', $options);
Access to the options in frontend:
Joomla.optionsStorage.plg_examplejs;
Links:
Labels |
Labels |
Added:
?
|
Status | New | ⇒ | Pending |
Build | ⇒ | . |
Labels |
Title |
|
Title |
|
Title |
|
Title |
|
It is similar to #6006 but #6006 is more compact
@Kubik-Rubik yes, this is still a very very very very valid feature request
at least I hope so
Milestone |
Added: |
@roland-d for now if you need to add some JavaScript variable for one of your script you do:
$doc->addScriptDeclaration("
var foo = 'blabla';
var bar = ['more', 'blabla'];
");
Such approach have couple problem:
$doc->_scripts
with some regex;With my suggestion you can do:
$doc->addScriptOptions('myextension_name', array(
'foo' => 'blabla',
'bar' => ['more', 'blabla']
));
and in javascript access to them:
console.log(Joomla.optionsStorage.myextension_name.foo);
console.log(Joomla.optionsStorage.myextension_name.bar);
As you can see all options under Joomla.optionsStorage.myextension_name
instead of global scope.
Also you can modify your options at PHP runtime easily, eg. from plugin or where you need to do it.
$options = $doc->getScriptOptions('myextension_name');
$options['foo'] = 'ue ue';
$doc->addScriptOptions('myextension_name', $options);
// or more easy:
$doc->addScriptOptions('myextension_name', array('foo' => 'ue ue'));
// it will be merged, and old 'foo' changed
Initially I made it for #1260, #6357, as with such approach we can move 99% of inline javascript to the files more easy, and rule them just by using the appropriate options in Joomla.optionsStorage
This PR has received new commits.
CC: @yvesh
@Fedik I was speaking with some PLT members about the possibility to use more the data attributes to pass strings or PHP values. This way we drop the inline script just to pass some simple (or more complex data) from PHP to Javascript. Although a way to do it with inline script MUST also be available. So my idea is to drop JText::script() and replace it with this function you introduce here, and make sure that in most places (at least in the core) we just use data attributes. But that is more a 4.0 thing I guess...
Milestone |
Removed: |
@dgt41 And what means your comment to someone who wants testing patches? No further tests on this one?
Category | ⇒ | Libraries |
Milestone |
Added: |
This PR has received new commits.
CC: @yvesh
I have tested this item successfully on 852311a
I have added the example array to the view.html.php file of com_content on the frontend. After that loaded the page and I was able to access the JavaScript options using the console editor.
I have tested this item successfully on 852311a
Status | Pending | ⇒ | Ready to Commit |
We have 2 successful tests, setting to RTC.
Labels |
Added:
?
|
Status | Ready to Commit | ⇒ | Needs Review |
Labels |
Setting to needs review. As the pretty print requires a php version higher than the joomla minimun
Labels |
Removed:
?
|
@brianteeman JSON_PRETTY_PRINT
in use when it available
$pretyPrint = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
Status | Needs Review | ⇒ | Ready to Commit |
Thanks for confirming that - all good - setting back to RTC
Labels |
Added:
?
|
Status | Ready to Commit | ⇒ | Fixed in Code Base |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2016-05-07 11:27:37 |
Closed_By | ⇒ | Kubik-Rubik |
Labels |
Removed:
?
|
Works without problems and offers a nice way to store javascript options
This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.