? ? Success

User tests: Successful: Unsuccessful:

avatar Fedik
Fedik
8 Feb 2014

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:

avatar Fedik Fedik - open - 8 Feb 2014
avatar brianteeman brianteeman - change - 8 Feb 2014
Labels
avatar brianteeman brianteeman - change - 8 Aug 2014
Labels Added: ?
avatar brianteeman brianteeman - change - 21 Aug 2014
Status New Pending
Build .
avatar nicksavov nicksavov - change - 21 Aug 2014
Labels
avatar jissues-bot jissues-bot - change - 17 Oct 2014
Title
[#31203] The one way and the one container for store the script options
The one way and the one container for store the script options
avatar brianteeman brianteeman - change - 17 Oct 2014
Title
[#31203] The one way and the one container for store the script options
The one way and the one container for store the script options
avatar yvesh yvesh - test_item - 14 Mar 2015 - Tested successfully
avatar yvesh
yvesh - comment - 14 Mar 2015

Works without problems and offers a nice way to store javascript options :+1:


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.
avatar Fedik Fedik - change - 21 Mar 2015
Title
The one way and the one container for store the script options
[#31203] The one way and the one container for store the script options
avatar joomla-cms-bot joomla-cms-bot - change - 21 Mar 2015
Title
The one way and the one container for store the script options
[#31203] The one way and the one container for store the script options
8935d55 21 Mar 2015 avatar Fedik c.s.
avatar Fedik Fedik - change - 21 Mar 2015
The description was changed
avatar anibalsanchez
anibalsanchez - comment - 6 Jun 2015

It is similar to #6006 but #6006 is more compact


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar Kubik-Rubik
Kubik-Rubik - comment - 29 Jul 2015

@Fedik Since we don't have a reaction for so long, is this still a valid feature request?

@dgt41 Could you please also review here? Thanks!

avatar Fedik
Fedik - comment - 29 Jul 2015

@Kubik-Rubik yes, this is still a very very very very valid feature request :wink:
at least I hope so :smile:

avatar Kubik-Rubik Kubik-Rubik - change - 6 Aug 2015
Milestone Added:
avatar roland-d
roland-d - comment - 23 Oct 2015

@Fedik I am not understanding the use of this PR. Can you explain it in more detail?

avatar Fedik
Fedik - comment - 24 Oct 2015

@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:

  • you cannot easily modify it at runtime , only by parsing $doc->_scripts with some regex;
  • this will add these variables in Global scope that can overlap the existing variable from another extension.

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

e2201e7 25 Oct 2015 avatar Fedik c.s.
avatar joomla-cms-bot
joomla-cms-bot - comment - 25 Oct 2015

This PR has received new commits.

CC: @yvesh


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar dgt41
dgt41 - comment - 25 Oct 2015

@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...

avatar wilsonge wilsonge - change - 6 Nov 2015
Milestone Removed:
avatar peterpeter
peterpeter - comment - 13 Nov 2015

@dgt41 And what means your comment to someone who wants testing patches? No further tests on this one?


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar brianteeman brianteeman - change - 30 Mar 2016
Category Libraries
avatar roland-d roland-d - change - 15 Apr 2016
Milestone Added:
avatar roland-d
roland-d - comment - 15 Apr 2016

@Fedik Can this be used next to what we have now? In that case we may be able to work on this for 3.6 as there seems general consensus this is the way forward.

avatar Fedik
Fedik - comment - 16 Apr 2016

@roland-d yes, it still valid,
I just need to check the conflicts, someday :smiley:

avatar joomla-cms-bot
joomla-cms-bot - comment - 17 Apr 2016

This PR has received new commits.

CC: @yvesh


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar roland-d
roland-d - comment - 30 Apr 2016

@Fedik I had another go at this PR and it's great. I now fully understand what it does and see great potential for it :) Thanks for the follow-up even though it has taken 2 years. Let's get it done for 3.6.

avatar roland-d roland-d - test_item - 30 Apr 2016 - Tested successfully
avatar roland-d
roland-d - comment - 30 Apr 2016

I have tested this item :white_check_mark: 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.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar laoneo laoneo - test_item - 6 May 2016 - Tested successfully
avatar laoneo
laoneo - comment - 6 May 2016

I have tested this item :white_check_mark: successfully on 852311a


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar roland-d roland-d - change - 7 May 2016
Status Pending Ready to Commit
avatar roland-d
roland-d - comment - 7 May 2016

We have 2 successful tests, setting to RTC.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar joomla-cms-bot joomla-cms-bot - change - 7 May 2016
Labels Added: ?
avatar brianteeman brianteeman - change - 7 May 2016
Status Ready to Commit Needs Review
Labels
avatar brianteeman
brianteeman - comment - 7 May 2016

Setting to needs review. As the pretty print requires a php version higher than the joomla minimun


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar joomla-cms-bot joomla-cms-bot - change - 7 May 2016
Labels Removed: ?
avatar Fedik
Fedik - comment - 7 May 2016

@brianteeman JSON_PRETTY_PRINT in use when it available :wink:

$pretyPrint  = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
avatar brianteeman brianteeman - change - 7 May 2016
Status Needs Review Ready to Commit
avatar brianteeman
brianteeman - comment - 7 May 2016

Thanks for confirming that - all good - setting back to RTC


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3072.

avatar joomla-cms-bot joomla-cms-bot - change - 7 May 2016
Labels Added: ?
avatar Kubik-Rubik
Kubik-Rubik - comment - 7 May 2016

Thank you @Fedik and all testers!

avatar Kubik-Rubik Kubik-Rubik - change - 7 May 2016
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
avatar Kubik-Rubik Kubik-Rubik - close - 7 May 2016
avatar Kubik-Rubik Kubik-Rubik - merge - 7 May 2016
avatar joomla-cms-bot joomla-cms-bot - close - 7 May 2016
avatar joomla-cms-bot joomla-cms-bot - change - 7 May 2016
Labels Removed: ?
avatar Fedik Fedik - change - 4 Sep 2016
The description was changed
avatar Fedik Fedik - edited - 4 Sep 2016

Add a Comment

Login with GitHub to post a comment