Re: One way JavaScript initialization (part 2)

247 views
Skip to first unread message

Fedir

unread,
Dec 29, 2012, 11:01:42 AM12/29/12
to joomla-...@googlegroups.com
no one liked/interesting this idea? :)

current state https://github.com/Fedik/joomla-cms/compare/2090e2f215...8510dff05c  - main changes it was make set/getOptions() little simple and use extend for optionsStorage



Середа, 19 грудня 2012 р. 14:02:21 UTC+2 користувач Fedir написав:
Part 1 here https://groups.google.com/d/topic/joomla-dev-platform/dWUbRsOAtNw/discussion 

Please check first there :)

I tried bring to life this idea ... check it here https://github.com/Fedik/joomla-cms/compare/0d6301002f...0151b9be0d
So for "init" own script the developers need do like next:

Joomla.addEvent('domready domchanged.plg_example', function(event, element){
  //get options
  var options = Joomla.optionsStorage.plg_example;
   //jquery developers
  jQuery('a', element).click(function(){ alret('Tada!');});
  //mootools developers
   document.id(element).getElemets('a').addEvent(function(){ alret('Tada!');});
})

and if need:

Joomla.addEvent('unload', function(event, element){
  // some code for clean up or something
})


And AJAX/AHAH developer do next:

before content will be changed:
Joomla.fireEvent('unload', 'oldElement');

after changes is done
Joomla.fireEvent('domchanged', element);


For current implementation need a MooTools .. 
If need make it independed then need in Joomla.addEvent change window.addEvent() to something own like:

if (nameBase.indexOf('ready') !== -1) {
 contentLoaded(window, fireEvent.bind(this, nameBase));
} else if (window.addEventListener) { // W3C DOM
 window.addEventListener(nameBase, fireEvent.bind(this, nameBase));
}
else if (window.attachEvent) { // IE DOM
 window.attachEvent("on" + nameBase, fireEvent.bind(this, nameBase));
}

+ write couple addittional methods that not exist in old IE (.bind(), Array.indexOf())


So any suggestion? How optimise my version or how make better implementation? 
Maybe need put it in to separate file, and really should make it independence from frameworks?

Fedir

unread,
Jan 12, 2013, 7:23:23 AM1/12/13
to joomla-...@googlegroups.com
I am back :)

after latest commits now it is independent from any framework https://github.com/Fedik/joomla-cms/compare/a388af85dc...f9bd4681cb

and as I seen the functions from a core.js can work without frameworks to, so in theory core.js can be moved out from behavior.framework in  behavior.core, and added as first script when some 'behavior', 'html.script' or 'addscrip()' executed .. but it theme for another discussion

back to 'One way JavaScript initialization'...
any critique? thoughts about how to make it better and more clearly (change naming, e,g, .addEvent() => .makeMePizza()  or something) ? 

I just got one, maybe better do like:
Joomla.addEvent('domready.extension_name', callback); //when need domready event
Joomla.addEvent('load.extension_name', callback); //when need load event
Joomla.addEvent('unload.extension_name', callback); //when need unload event
instead of I suggested before:
Joomla.addEvent('domready domchanged.extension_name', callback); //when need domready event
Joomla.addEvent('load domchanged.extension_name', callback); //when need load event
and in Joomla.addEvent() we automatically subscribe all domready/load callbacks also to the domchanged event..
then it will be more clearly for end developers no?

also using extension_name we can take script options from optionsStorage and pass it in to callback, so callback will looks like function(event, element, options) ... but not sure whether it have a sense, because the options always available by Joomla.optionsStorage.extension_name


Субота, 29 грудня 2012 р. 18:01:42 UTC+2 користувач Fedir написав:

Fedir

unread,
Feb 8, 2013, 12:04:50 PM2/8/13
to joomla-...@googlegroups.com
now it with auto subscription to 'domchanged' event...
https://github.com/Fedik/joomla-cms/compare/e1a9541...982982a

Субота, 12 січня 2013 р. 14:23:23 UTC+2 користувач Fedir написав:

Fedir

unread,
Feb 24, 2013, 7:28:20 AM2/24/13
to joomla-...@googlegroups.com
ok,  I found couple problems with my previous realization, and think the dance around  'domchanged' event was to tricky ... 

in current realization:
 - automatically subscribe all "domready" events to the "load" event
 - use Joomla.fireEvent('load', element) for notify the script about the changes in DOM 
 - all "initial" events put in to the "jinit" namespace, that will fire on "initial" "domready" or "load" ..... this allow divide the "domready" event subscribed to "load" and "load" event 
 - use namespace tree, without any the depth limitation
 - it is the framework independent, so can work if no framework connected and used only pure JavaScript

now for the init we just use:
Joomla.addEvent('domready', callback);

instead of mixes with:
jQuery(document).ready(callback);
and
window.addEvent('domready', callback);

after some DOM manipulation we need tell for a scripts that we changed something, like:
Joomla.fireEvent('unload', 'element-that-will-be-changed') // before changes
Joomla.fireEvent('load', 'changed-element') // after changes 

that not possible if used mixed initialization (that is the main cause of current suggestion)...

this + the namespace tree allow more simple interact between different extensions, if needed 

namespace example:
we have:
Joomla.addEvent('domready.extension1', callback1)
Joomla.addEvent('domready.extension1.gallery', callback2)
Joomla.addEvent('domready.extension1.slider', callback3)
Joomla.addEvent('domready.extension2', callback4)

here on "domready" will be called all these callbacks ...

if after some DOM manipulation we need re-init only the  "extension1" and they children, then we can just fire:
Joomla.fireEvent('load.extension1', 'changed-element')

if need re-init only callback2 then just fire:
Joomla.fireEvent('load.extension1.gallery', 'changed-element')

There no limitation for the depth of the namespace.
Also there no limitation for used events, so can be used the "fake events" ... not sure that it will use some one, it's just a side result of the current implementation :)

in next hour I will try update post on  joomlacode site and upload the plugin that I used for testing and some simple demonstration module


Пʼятниця, 8 лютого 2013 р. 19:04:50 UTC+2 користувач Fedir написав:

Fedir

unread,
Feb 24, 2013, 8:19:18 AM2/24/13
to joomla-...@googlegroups.com
the module for simple demonstration http://joomlacode.org/gf/download/trackeritem/28119/78789/mod_examplejsinit-2013.02.18.tar.gz

Неділя, 24 лютого 2013 р. 14:28:20 UTC+2 користувач Fedir написав:

Roberto Segura

unread,
Mar 15, 2013, 10:53:15 AM3/15/13
to joomla-...@googlegroups.com
Hi Fedir,

I read this some time ago but never answered. I should :P

I really like the idea of an independent Joomla class. Currently any JS that loads translations has to use mootools. So it would be good to make translations available to any framework.

I also like the JS custom events :)

We have created a group to work on the installer (mainly JS) and I think that you can bring some cool ideas that will help also the rest of the CMS.

Are you interested in joining?

Thanks!

Fedir

unread,
Apr 27, 2013, 1:41:53 PM4/27/13
to joomla-...@googlegroups.com
Hi Roberto,

Sorry, I missed you message for some reason ...

it is still actual? :)


Пʼятниця, 15 березня 2013 р. 16:53:15 UTC+2 користувач Roberto Segura написав:

Fedir

unread,
Jun 9, 2013, 9:37:05 AM6/9/13
to joomla-...@googlegroups.com
pull request https://github.com/joomla/joomla-cms/pull/1260


Субота, 27 квітня 2013 р. 20:41:53 UTC+3 користувач Fedir написав:

Roberto Segura

unread,
Jun 10, 2013, 2:00:10 PM6/10/13
to joomla-...@googlegroups.com
This is great Fedir :)

Can you add me to Skype? My id is phproberto
Reply all
Reply to author
Forward
0 new messages