User tests: Successful: Unsuccessful:
This patch will improve the functionality of Joomla.JText._()
javascript function by supporting dynamic variable declaration easily.
Let's try to get article title in javascript file.
components/com_content/views/article/tmpl/default.php
JText::scriptVar('article', $this->item->title);
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.
Full b/c, no problems are expected
Try to follow the given example.
Labels |
Added:
?
|
Labels |
Added:
?
|
Category | ⇒ | JavaScript |
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.
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. :)
Labels |
Added:
?
|
Labels |
Added:
?
|
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.
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.
@text OK. Cool addition.
Sorry, but I have to agree with @bembelimen here. This screams for misuse.
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.
Thank you guys for review. It's better to close this PR then as per discussion it's going to abuse JText class.
Status | Pending | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2015-06-07 07:20:13 |
Closed_By | ⇒ | gunjanpatel |
Why not using "normal" JS?
alert(Joomla.MyNamespace.article);
I'm not sure about abusing language function for "normal" variables.