? Success

User tests: Successful: Unsuccessful:

avatar dgt41
dgt41
5 Nov 2015

A better way to pass PHP vars to Javascript

Instead of using addScriptDeclaratio() which comes with much computation power we can use data attributes to pass all the vars. Then we collect them easily from the javascript.

As an example Isis passes two variables that control the toolbar buttons (if it is sticky and the height of the menu). Now these vars are injected in a null div and collected from the static script.

Testing

Apply the patch and play with the different options that isis provides in it’s configuration.

Votes

# of Users Experiencing Issue
0/1
Average Importance Score
1.00

avatar dgt41 dgt41 - open - 5 Nov 2015
avatar dgt41 dgt41 - change - 5 Nov 2015
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 5 Nov 2015
Labels Added: ?
avatar Fedik
Fedik - comment - 5 Nov 2015

no bad, but you use id :neckbeard: hahaha

and I not sure whether that it is good: to add extra element just for keep JS options ...
it doesn't sounds good ... it force one more DOM action "search for element" ...
you know that work with DOM always slow part of JS

how this idea will work when we have 100 small scripts? :wink:
we will get bunch of empty divs...

the data attribute should be in element for which it related, in current case it is for .subhead element, if I right ... then it will looks as nice use of data :wink:

avatar dgt41
dgt41 - comment - 5 Nov 2015

the data attribute should be in element for which it related, in current case it is for .subhead element, if I right ... then it will looks as nice use of data

Yep that’s better

avatar andrepereiradasilva
andrepereiradasilva - comment - 5 Nov 2015

I agree! Replacing the inline js that can be put in a external js file for HTML5 data-* attributes is the way to go. Especially in the frontend.

For instance, i think this (https://github.com/joomla/joomla-cms/blob/staging/libraries/cms/html/behavior.php#L667-L673) can be done with two data-* attributes in, for instance, body tag.
Example: <body ... data-keepalive-method="frontend" data-keepalive-time="360000">
And the js function moved to a joomla core js file but reading the values from body data-keepalive-* attributes.

@dgt41: sorry to hijack the PR but i think it's related with what you are doing.

avatar Fedik
Fedik - comment - 5 Nov 2015

@dgt41 yes this is better, but still can be better :wink:
you still use id, and now search for it twice, at least :smile:
and as here is jQuery already involved then no need to afraid of it :ghost:

here is quick suggestion:

...
<div class="subhead" data-tmpl-sticky="<?php echo $stickyBar; ?>" data-tmpl-offset="<?php echo $offset; ?>">
...
var navTop,
    isFixed = false,
    $subhead = $('.subhead');

if ($subhead.data('tmplSticky')) {
    processScrollInit();
    processScroll();

    $(window).on('resize', processScrollInit);
    $(window).on('scroll', processScroll);
}

function processScrollInit() {
    navTop = $subhead.offset().top - $subhead.data('tmplOffset');

    // Fix the container top
    $(".container-main").css("top", $subhead.height() + $('nav.navbar').height());

    // Only apply the scrollspy when the toolbar is not collapsed
    if (document.body.clientWidth > 480) {
        $('.subhead-collapse').height($subhead.height());
        $subhead.scrollspy({offset: {top: $subhead.offset().top - $('nav.navbar').height()}});
    }
}
function processScroll() {
    var scrollTop = $(window).scrollTop();
    if (scrollTop >= navTop && !isFixed) {
        isFixed = true;
        $subhead.addClass('subhead-fixed');

        // Fix the container top
        $(".container-main").css("top", $subhead.height() + $('nav.navbar').height());
    } else if (scrollTop <= navTop && isFixed) {
        isFixed = false;
        $subhead.removeClass('subhead-fixed');
    }
}
avatar dgt41
dgt41 - comment - 5 Nov 2015

@Fedik I agree for the use of a var instead of searching for the element all the time. I am not sure about the class, id’s were faster once in the world wild web ????

@andrepereiradasilva yes we are in the same line here. There are a lot of places that this can be applied in current joomla’s codebase

avatar Fedik
Fedik - comment - 5 Nov 2015

@dgt41 the idea just "reuse the thing that already in use" :wink:
means, if you handle element .subhead, why do you need to search for another element? just for take the data properties? it does not looks efficient :wink:

upd. maybe I am too picky, sorry :wink:

avatar zero-24 zero-24 - change - 7 Nov 2015
Category JavaScript Templates (admin)
avatar zero-24 zero-24 - change - 7 Nov 2015
Easy No Yes
avatar erikvandoorne
erikvandoorne - comment - 15 Apr 2016

Tested all options in the ISIS template and all options can be altered and saved correctly. Test passed OK.


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

avatar erikvandoorne erikvandoorne - test_item - 15 Apr 2016 - Tested successfully
avatar erikvandoorne
erikvandoorne - comment - 15 Apr 2016

I have tested this item :white_check_mark: successfully on 5dc0f9a

Tested all options in the ISIS template and all options can be altered and saved correctly. Test passed OK.


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

avatar insitevision insitevision - test_item - 15 Apr 2016 - Tested successfully
avatar insitevision
insitevision - comment - 15 Apr 2016

I have tested this item :white_check_mark: successfully on 5dc0f9a

Tested all options in ISIS, all options work correctly.


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

avatar brianteeman brianteeman - change - 15 Apr 2016
Status Pending Ready to Commit
avatar brianteeman
brianteeman - comment - 15 Apr 2016

RTC


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

avatar joomla-cms-bot joomla-cms-bot - change - 15 Apr 2016
Labels Added: ?
avatar brianteeman brianteeman - change - 15 Apr 2016
Milestone Added:
avatar rdeutz rdeutz - change - 2 May 2016
Status Ready to Commit Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2016-05-02 06:01:13
Closed_By rdeutz
avatar rdeutz rdeutz - close - 2 May 2016
avatar rdeutz rdeutz - merge - 2 May 2016
avatar joomla-cms-bot joomla-cms-bot - close - 2 May 2016
avatar joomla-cms-bot joomla-cms-bot - change - 2 May 2016
Labels Removed: ?

Add a Comment

Login with GitHub to post a comment