[4.0] Client side, javascript Behavior API#6357
[4.0] Client side, javascript Behavior API#6357Fedik wants to merge 43 commits intojoomla:stagingfrom
Conversation
|
Everybody, a great discussion but we can't seem to reach an agreement as to how to move forward with this. I am going to close this now but feel free to re-open if you plan to continue working on this. Thank you for your cooperation. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6357. |
|
@roland-d can you add a re evaluate for 4 tag here? This is totally needed IMHO |
|
Going to reopen so it will be seen even though it is a reevaluate fod 4 |
|
@mbabker for [4.0]-Projects? This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/6357. |
|
@Fedik can you remove the DomReady? (you can safely user the native |
| { | ||
| var $target = $(event.target); | ||
| $target.find('label.template-isis-radio').removeClass('template-isis-radio').off('click.templateIsisRadio'); | ||
| }); |
There was a problem hiding this comment.
vanilla
var targets = [].slice.call(event.target.querySelectorAll('label.template-isis-radio''));
targets.forEach(function(item) {
item.classList.remove('template-isis-radio').off('click.templateIsisRadio');
});
|
@DGT41 I need some input here, what do you think I have made possibility that the behavior can be loaded on the page all the time, but called only when the Joomla.Behavior.add('behaviorname', 'ready update', function(event){
console.log(event.name, event.target);
}, true <===);I think about change it to: Joomla.Behavior.add('behaviorname', 'ready update', function(event){
console.log(event.name, event.target, event.options);
}, 'script-options-key-blablabla' <===);it allow to put the behaviors in a single file, load it to the page, and they will be executed only if Or I just remove such possibility? |
|
So the ready event corresponds to document.('DOMContentLoaded') ? What happens if scriptOptions not there? Crash and burn? |
yes right
blue screen with random bright yellow words on it, obviously 😉 well, no Joomla.Behavior.add('do-stuff', 'ready update', function(event){
console.log(event.name, event.target, event.options);
event.target.style.background = event.options.background || '';
}, 'do-stuff-options-key');for make it run you $doc->addScriptOptions('do-stuff-options-key', array('background' => 'black'))so at time of OR can call it via your another script (or from another extension), with your custom options: var changedContainer = document.getElementById("main-container")
Joomla.Behavior.call('update.do-stuff', changedContainer, {background:'yellow'});if there no options at time of |
Conflicts: administrator/components/com_modules/views/modules/view.html.php administrator/templates/isis/js/template.js libraries/cms/html/behavior.php libraries/cms/html/bootstrap.php libraries/cms/html/formbehavior.php libraries/joomla/document/document.php libraries/joomla/document/html/renderer/head.php media/system/js/core-uncompressed.js media/system/js/core.js media/system/js/repeatable-uncompressed.js media/system/js/repeatable.js templates/protostar/js/template.js tests/unit/suites/libraries/cms/html/JHtmlBehaviorTest.php tests/unit/suites/libraries/cms/html/JHtmlBootstrapTest.php tests/unit/suites/libraries/cms/html/JHtmlFormbehaviorTest.php
|
I guess the idea is tooooo complicated 😄 "javascript Behavior API" is dead, long live "Joomla! Custom events"! See #16016 |
Since version 1.5 Joomla made huge step to split HTML and PHP Logic, but JavaScript still in "younger brother" role, and very hard coded.
Here is a try to split PHP and JavaScript.
You ask: How that possible?
Very simple: Write PHP in PHP file, and JavaScript in JS file
You ask: Then how to connect them?
Well, using JSON, maybe it not the best solution but also not the worst. 👽
So
This pull request introduce
Joomla Behavior APIfor client side.It:
Also introduce
JDocument::setScriptOptions()that is important gear in connection betwen PHP Logic and client side.Each Behavior could be called only if server provide the options for that Behavior, or always.
How it work.
Each Behavior it just a collection of callbacks that called when corresponding event happen.
All behavior could be stored in js files, and splitted by group:
behavior-bootstrap.js,behavior-form.jsso on.Register custom behavior:
This will be called only if Server provide options for this behavior
$doc->addScriptOptions('behaviorname', array(/*some options*/)), and when happened next events:ready,updateRegister custom behavior that will be always called:
You asked: wtf is
updateevent?update- is a custom event that allow to call one Behavior from another "on fly", also this event must be called if you replace part of DOM, so other Behaviors will know about changes.There also
removecustom event, that could be used to ask Behaviors for clean up in 'container', unbind events and so on.This events we could rename of course to something more clear, if need 😄
And, of course, anyone could use any custom event that need for his/her needs.
Example:
How it work on Live you can look in repeatable field in repeatable.js (see diff)
How to override existing Behavior.
Very simple, just register own behavior with same name, and it will override any existing:
or you could override only specific event callback if need:
B/C
I tried make it for keep b/c, with count that it could be in the core since ~~3.~~5.0
So you should not see any difference after apply this patch.
Conclusion
Split PHP logic and JavaScript is a small step to big improve 😋
Next step could be dependency management using Requirejs or anything else.
Well to much text, I hope here understandable at least couple words
ping @phproberto @DGT41 @okonomiyaki3000 @anibalsanchez I need your brains 👻
ps. couple word about jquery 😋