Skip to content

Comments

[#31203] The one way and the one container for store the script options#3072

Merged
Kubik-Rubik merged 14 commits intojoomla:stagingfrom
Fedik:scrip-options-storage
May 7, 2016
Merged

[#31203] The one way and the one container for store the script options#3072
Kubik-Rubik merged 14 commits intojoomla:stagingfrom
Fedik:scrip-options-storage

Conversation

@Fedik
Copy link
Member

@Fedik Fedik commented Feb 8, 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:

@jissues-bot jissues-bot changed the 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 Oct 17, 2014
@yvesh
Copy link
Member

yvesh commented Mar 14, 2015

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.

@joomla-cms-bot joomla-cms-bot changed the 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 Mar 21, 2015
@anibalsanchez
Copy link
Contributor

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.

@Kubik-Rubik
Copy link
Member

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

@Fedik
Copy link
Member Author

Fedik commented Jul 29, 2015

@Kubik-Rubik yes, this is still a very very very very valid feature request 😉
at least I hope so 😄

@Kubik-Rubik Kubik-Rubik added this to the Joomla! 3.5.0 milestone Aug 6, 2015
@roland-d
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires a blank line before the if statement according to codestyle.

@Fedik
Copy link
Member Author

Fedik commented Oct 24, 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

@joomla-cms-bot
Copy link

This PR has received new commits.

CC: @yvesh


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

@dgrammatiko
Copy link
Contributor

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

@wilsonge wilsonge removed this from the Joomla! 3.5.0 milestone Nov 6, 2015
@peterpeter
Copy link
Contributor

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

@roland-d
Copy link
Contributor

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

@Fedik
Copy link
Member Author

Fedik commented Apr 16, 2016

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

Conflicts:
	libraries/joomla/document/html/renderer/head.php
@joomla-cms-bot
Copy link

This PR has received new commits.

CC: @yvesh


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

@roland-d
Copy link
Contributor

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

@roland-d
Copy link
Contributor

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.


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

@laoneo
Copy link
Member

laoneo commented May 6, 2016

I have tested this item ✅ successfully on 852311a


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

@roland-d
Copy link
Contributor

roland-d commented May 7, 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.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label May 7, 2016
@brianteeman
Copy link
Contributor

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.

@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label May 7, 2016
@Fedik
Copy link
Member Author

Fedik commented May 7, 2016

@brianteeman JSON_PRETTY_PRINT in use when it available 😉

$pretyPrint  = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);

@brianteeman
Copy link
Contributor

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.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label May 7, 2016
@Kubik-Rubik
Copy link
Member

Thank you @Fedik and all testers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.