? ? Success

User tests: Successful: Unsuccessful:

avatar gunjanpatel
gunjanpatel
7 Feb 2015

This patch will improve the functionality of Joomla.JText._() javascript function by supporting dynamic variable declaration easily.

For Example:


Let's try to get article title in javascript file.

  • In components/com_content/views/article/tmpl/default.php
JText::scriptVar('article', $this->item->title);
  • Use this variable in javscript file or declaration
jQuery(window).on('load',  function() {
    // Print in console
    console.log(Joomla.JText._('ARTICLE'));

    // Or try to alert the value
    alert(Joomla.JText._('ARTICLE'));
});

This will print article title in browser console.

Backwards compatibility


Full b/c, no problems are expected

Testing instructions


Try to follow the given example.

avatar gunjanpatel gunjanpatel - open - 7 Feb 2015
avatar joomla-cms-bot joomla-cms-bot - change - 7 Feb 2015
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - change - 7 Feb 2015
Labels Added: ?
avatar zero-24 zero-24 - change - 7 Feb 2015
Category JavaScript
avatar bembelimen
bembelimen - comment - 8 Feb 2015

Why not using "normal" JS?

$js = 'Joomla.MyNamespace.article = "' . $this->item->title . '";';
JFactory::getDocument()->addScriptDeclaration($js);
alert(Joomla.MyNamespace.article);

I'm not sure about abusing language function for "normal" variables.

avatar gunjanpatel
gunjanpatel - comment - 9 Feb 2015

Reasons:

  1. Just imagine there are 20 of dynamic variable in your view source declared like this then output will look like,

    Joomla.MyNamespace.article = 'title';
    Joomla.MyNamespace.article_name = 'name';
    Joomla.MyNamespace.somethingelse = 'xyz';
    ... and so on
    

    When using this function it will just append one key and value in JSON string.

  2. We already have language store, this function is appending more string keys and values. Which will make it easy in use. You will just need JText::scriptVar($key, $value); in php file and use it anywhere in JS. Similarly, we already have JText::script(JText::_('SOME_LANGUAGE_CONSTANT));.

I didn't see any abuse of this function as it's using the same string store without disturbing JText::script functionality. :)

avatar roland-d roland-d - change - 10 Feb 2015
Labels Added: ?
avatar roland-d roland-d - change - 10 Feb 2015
Labels Added: ?
avatar compojoom
compojoom - comment - 14 Mar 2015

works as advertised, but I'm not sure that the JText class should be misused in this way.
JText is created for text and not for just storing anything, because it offers a json object.
I have the feeling that some developers will misuse this to store some configuration, instead of just use it for language constants.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6006.
avatar compojoom compojoom - test_item - 14 Mar 2015 - Tested successfully
avatar gunjanpatel
gunjanpatel - comment - 16 Mar 2015

Thanks for test @compojoom
I understand, but that is depends on developer it self. If developer is stupid enough to store password in json object then no body will able to stop him. This function is intended to give easier way to put something into PHP and get it easily into JS. So, I think developer must know what he should share in json. This function is not setting anything by it's own.

avatar anibalsanchez
anibalsanchez - comment - 6 Jun 2015

@text OK. Cool addition.


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

avatar anibalsanchez anibalsanchez - test_item - 6 Jun 2015 - Tested successfully
avatar Hackwar
Hackwar - comment - 6 Jun 2015

Sorry, but I have to agree with @bembelimen here. This screams for misuse.

avatar chmst
chmst - comment - 6 Jun 2015

Please keep the Joomla Code as clean as possible. Abusing a class is always bad practice and will cause misunderstandings and errors in the future.

avatar anibalsanchez
anibalsanchez - comment - 6 Jun 2015

I have noticed a similar purpose in #3072

avatar gunjanpatel
gunjanpatel - comment - 7 Jun 2015

Thank you guys for review. It's better to close this PR then as per discussion it's going to abuse JText class.

avatar gunjanpatel gunjanpatel - change - 7 Jun 2015
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2015-06-07 07:20:13
Closed_By gunjanpatel
avatar gunjanpatel gunjanpatel - close - 7 Jun 2015

Add a Comment

Login with GitHub to post a comment