[#28119] Add some standard for JavaScript initialisation + options storage#1260
[#28119] Add some standard for JavaScript initialisation + options storage#1260Fedik wants to merge 25 commits intojoomla:masterfrom
Conversation
|
This is interesting. Did you think about delegated events? |
|
yes, there already was suggestion about using the delegation.... |
…Object.append for options
…ead of "domchanged"
…for namespaced events.
…e for fire "initial" events.
…re that marker "readyCalled" set .
|
I like the setScriptOptions idea, especially where options are passed in as php objects and later its json encoded out, which is safer than script declarations. A lot of developers pass in js options via addScriptDeclaration and whenever there's an error in their script it just wrecks subsequent script declarations that comes after it. And the addEvent/fireEvent too, this really tidy things up because 3rd party extensions expects you to listen to their events on their html elements (or they probably provide their own custom event api) which gets really messy because everyone's doing their own thing. But I also think it would be interesting to see a Deferred variation of the addEvent/fireEvent. This is because the event might get fired first before the person that listens to the event attaches to the event. This is especially true for sites with js compressor plugins installed which they tend to mess up the ordering of script execution. |
|
the one container for store the script options moved to the separated pull #11319 |
|
good question ;) current realization work without any JS library, so should be no any conflict with jQuery or something ... I thought about next additional changes in CMS (if we use such idea):
but: //event subscription
jQuery(document).on('jload.extension', callback);
//when something changed, we fire event on changed element:
jQuery('unloaded-element').trigger('junload');
//in the callback we take changed element
function callback(event){
var changed = event.target;
}other possible solution it is use "Signals"... this topic required more feedback as we have now, |
|
First of all, please fix spelling mistakes in your code. In the PHP code, you have otions instead of options. Second of all, if you still want this to be merged in, please update your branch to the latest of the staging branch. Besides those 2 points, I don't think this is a good idea. As I already wrote on the mailinglist, I don't think it is a good idea to introduce yet-another-abstraction for the javascript just in case someone wants to use obscure-JS-framework XY instead of jQuery. We should also not branch into the area of writing our own JS framework with event handling etc. That is not our area of expertise and we would only re-invent the wheel here. That said, I would reject this PR. |
Conflicts: media/system/js/core.js
in which? 😉
without re-invent the wheel Humans would live in caves and use the wagons instead of the cars 😃
I think you did not understand the purpose of my suggestion. Maybe the realisation is not perfect, but currently I did not found a better solution, if someone has a better idea please share. |
|
Hello @Fedik, I wrote that one the joomla-dev-cms mailinglist. I looked through your description and the method names again and I still can't see the difference between the event system of jQuery and this. You can create your own events in jQuery, too, and you can attach your own code to it, too. Considering that we had discussions in the past that proposed to introduce a compatibility layer between Joomla and the Javascript library of choice, I wrote that we should concentrate on what is pur area of expertise and not venture into writing our own JS library. Joomla uses jQuery as a standard and is moving away from mootools. There is no reason for us to integrate another JS library or write our own. |
|
@Hackwar sorry I wrong understand you, Well, when I started it, we use Mootools, and migration to jQuery only started. Of course such API possible with jQuery, I already tried something jsinit-jq ... |
|
Again: We standardized everything on jQuery now. We will stick with jQuery for the forseeable future and jQuery and its syntax is EXTREMELY widely adopted, thus people know it deeply. You are asking us to introduce yet another JS library, that is not widely tested, not known at all and which would mean that extension developers again have to change their code. Besides that, the code currently does not even remotely cover the functionality of jQuery and thus people will include jQuery, too. That again means that not only do we have the larger core.js file, but also jQuery on every page. |
It not really
I did not tried "write own jQuery".
yes of course 😄
I agree, but ... wait I will write you some example.... |
|
@Hackwar let`s imagine, you write a very cool Gallery plugin. In your code for initialise the gallery script you use: jQuery(document).ready(function($){
$('.cool-gallery').myCoolGallery(/*dynamic options*/);
})Well, all works perfect, I installed it on my site and I am happy. But now I found the template for my site, where the content loaded using AJAX request. jQuery(document).ready(function($){
$('#menu ul.menu a').on('click', function(){
$('#content-container').load($(this).attr('href'), {tmpl: 'ajax'});
});
})All works perfect, until I tried open page where the Gallery, and nothing works there, what the...? (I think) Now the 🍺 question what I should do? P.S.: same problem with the repeatable field, when the field use some javascript gets broken when used in repeatable field. |
|
I don't believe this should be merged as we are standardizing on jQuery as mentioned by Hannes. Let's see what @wilsonge and @Bakual think of this. Guys, please check and let me know if you agree on closing this PR. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/1260. |
|
I'm unsure tbh. On one hand we are standardising on jQuery - but on the other jQuery is already quickly becoming old hat now better javascript support is coming in through the browsers. Many libraries no longer require jQuery anymore. So many places we're just using jQuery for checking domReady and then native javascript might be nice to have 'our own' way of doing it. |
|
@Fedik After discussing this in the PLT, we have decided that this should not be added to the core. Thank you for your contribution and ideas. |
This path:
The one way for a scrip initialization:
The scrip initialization works via the name spaced events. This is allow more interaction between a different extensions and their scripts.
For initialization need use just:
instead of:
And for tell for the other scripts about changes in DOM, need just:
The name spaced events allow more interaction.
Example we have:
At time "domready" event will be called all these callbacks.
if after some DOM manipulation we need re-init only the "extension1" and they children, then we just fire:
if need re-init only callback2 then just fire:
also can be passed more arguments if need:
Here no limitation for the depth of the namespace.
Also there no limitation for used events, so can be used the "fake events" ...
The one container for store the scrip options:
moved to pull #11319
This allow for the developers to move the all initialization code in to the separate file, and use the options storage for change the initialization logic.
The init file for plg_examplejs, example:
Some more thoughts:
This path require the more additional changes in CMS, of course.
Example: load the core.js on top of the all scripts, change all Joomla behaviors.
Also would be good to put whole core.js in self executable function for prevent direct access to some Joomla variables.
Some more at the links.
Links: