[RCD] initializing javascript in plugins

Brendan brendan at tucows.com
Tue Dec 9 23:49:33 CET 2014


i've run into a problem a couple times now. following the instructions
at http://trac.roundcube.net/wiki/Doc_Plugins for a plugin with
javascript that needs to register javascript hooks does not always work
- calling rcmail.addEventListener() can fail as the browser has not yet
finished compiling the rcube javascript code/object (chrome seems to
trigger this often, probably due to some compilation optimizations it has).

for a small plugin, i just added a setTimeOut loop that would only
execute once rcube had been defined, but it's a bit of an ugly hack (and
could open up some timing issues with respect to when the code actually
runs).

with this mobile plugin i ran into the problem again. locally, i added a
browser/jquery event at the beginning of rcube.init() (in app.js):
$.event.trigger({'type':'rcmail_init'});

the plugin javascript then just has a section:
$(document).on('rcmail_init', function() {
  // register rcube event listeners
});

it fires before pretty much anything else, which lets the plugin's
javascript add roundcube event listeners before they might otherwise
fire (later on inside init).

this solves the problem, but i had been meaning to ask if anyone had any
better solutions to this problem already (preferably that don't involve
updating roundcube to include a new event trigger). does anyone have any
ideas?

On 14-12-09 02:33 PM, Brendan wrote:
> oh. i know what's wrong. i tweaked something in app.js to work around a
> problem i was having with javascript execution order. let me see if i
> can fix that.
> 
> if you want to use it as it, add this line to program/js/app.js:
>  150     $.event.trigger({'type':'rcmail_init'});
> 
> (the problem is that the rcmail object is not always available when the
> script starts compiling)


More information about the dev mailing list