JPathRu / attributes

Компонент реализует дополнительные атрибуты для элементов системы, меню, материалов, категорий, модулей и плагинов.
GNU General Public License v3.0
15 stars 2 forks source link

Always use the API for the assets #1

Closed dgrammatiko closed 5 years ago

dgrammatiko commented 5 years ago

This https://github.com/JPathRu/attributes/blob/2fb273cb6860223d0ccee16e5ae86bf60d04a3ac/com_attrs/views/item/tmpl/edit.php#L12

will work only if the core.js gets loaded before this part of the page gets parsed (eg core.js in the head). If in my template the core.js script is called on the end of the body your code will not work as it will be overrides by core.js. Order of execution matters in JS, thus ALWAYS use the Joomla API for css and javascript (inline or files)...

b2z commented 5 years ago

Do you mean this? https://docs.joomla.org/J3.x:Adding_JavaScript_and_CSS_to_the_page

Septdir commented 5 years ago

@b2z like that

Factory::getDocument()->addScriptDeclaration("
    Joomla.submitbutton = function (task) {

        var msg_sn = '', msg_dest = '', msg_valid = '';

        var sn = /^[a-z_]+$/.test(document.getElementById('jform_name').value);

        var ds = document.getElementById('jform_destsystem0').checked;
        var dn = document.getElementById('jform_destmenu0').checked;
        var da = document.getElementById('jform_destarticles0').checked;
        var dc = document.getElementById('jform_destcategories0').checked;
        var dm = document.getElementById('jform_destmodules0').checked;
        var dp = document.getElementById('jform_destplugins0').checked;

        var dest = ds || dn || da || dc || dm || dp;

        var valid = document.formvalidator.isValid(document.id('item-form'));

        if (task == 'item.cancel' || (sn && dest && valid)) {
            Joomla.submitform(task, document.getElementById('item-form'));
        } else {
            if (!sn) {
                msg_sn = '" . $this->escape(Text::_('COM_ATTRS_NAME_ERROR')) . "';
            }
            if (!dest) {
                msg_dest ='" . $this->escape(Text::_('COM_ATTRS_DEST_ERROR')) . "';
            }
            if (!valid) {
                msg_valid = '" . $this->escape(Text::_('JGLOBAL_VALIDATION_FORM_FAILED')) . "';
            }

            Joomla.JText.load({error:'" . $this->escape(Text::_('ERROR'))."});
            Joomla.renderMessages({\"error\":[msg_sn, msg_dest, msg_valid]});
        }
    }
");

I did not check that code. So there may be mistakes.

AlekVolsk commented 5 years ago

RU: В этом нет никакой необходимости, т.к. 1) данный скрипт выводится только в шаблоне компонента в административной части где core.js, всегда подгружен; 2) этот скрипт всегда сработает, т.к. он не зависит от core.js, он зависит только от скрипта валидации, подгруженного тремя строками ранее. В любом случае, этот скрипт - просто расширение базового примера, взятого из стандартных компонентов Joomla.

EN (google translated): There is no need for this, since 1) this script is displayed only in the component template in the administrative part where core.js is always loaded; 2) this script will always work, because it does not depend on core.js, it depends only on the validation script loaded in three lines earlier. In any case, this script is just an extension of the basic example taken from standard Joomla components.

dgrammatiko commented 5 years ago

You are WRONG!

this script is displayed only in the component template in the administrative part where core.js is always loaded;

The core.js is loaded in the HEAD only in CORE templates and that could also change...

this script will always work, because it does not depend on core.js, it depends only on the validation script loaded in three lines earlier.

You have zero clue what your code even overrides, take. look at https://github.com/joomla/joomla-cms/blob/36e923a869f692ad730e80d683e56e2df62a96e1/media/system/js/core-uncompressed.js#L98. You are overriding core.js, so if core.js gets loaded after your script your code will never execute as the one from core.js WILL override it!!!

Also the code that you're referring HTMLHelper::_('behavior.formvalidation'); will enqueue the script, where that will be echoed in the template is not Witten in stone, it could be in the head (then your code will work) or anywhere else (eg before </body> and then your script will throw a nice error...

Just use JFactory::getDocument()->addScriptDeclaration(YOUR_CODE_HERE);, simple and will always work as it meant to be

AlekVolsk commented 5 years ago

All for the sake of users' peace of mind

dgrammatiko commented 5 years ago

Thanks and sorry for being hard on this one but it's a common pitfall that needs some attention...