AJS.toInit is getting called before plugin has loaded

imamudin naseem April 10, 2018

I have written a plugin which shows up as Web panel in the right bar of issue page.

I have all my js written inside  AJS.toInit function since I want to make sure DOM tree of plugin is ready before execution of javascript.

AJS.toInit(function(){

// JS Code 

})

Plugin loads perfectly fine on view issue page (i.e /jira/browse/APPS-1) but when issue is opened on projects page (i.e /jira/projects/APPS/issues/APPS-1?filter=allopenissues), AJS.toInit function gets fired before plugin has loaded, and since I am doing something like $(".element-in-plugin"), it fails because $(".element-in-plugin") is not present in DOM

 

My velocity template file is as below

$webResourceManager.requireResource("com.atlassian.flock.jiraPlugin:jiraPlugin-resources")

<div id="jira-plugin" class="fp hidden">

</div>

I have defined web-resource as below 

<web-resource key="jiraPlugin-resources" name="jiraPlugin Web Resources">
<dependency>com.atlassian.auiplugin:ajs</dependency>
<dependency>com.atlassian.auiplugin:aui-select2</dependency>
<resource type="download" name="jiraPlugin.css" location="/css/jiraPlugin.css"/>
<resource type="download" name="jiraPlugin.js" location="/js/jiraPlugin.js"/>
<resource type="download" name="images/" location="/images"/>
<context>atl.general</context>
</web-resource>

 

 

3 answers

2 votes
Matthew Beda October 15, 2018
AJS.$('.element-in-plugin').ajaxComplete(function(){
//...Your Code
});
Tomislav Nikolic December 10, 2018

This is the way to go. I was ripping my hair out tryna make new_content_added work. AjaxComplete (I use ajaxStop, don't know if it's better) works flawlessly. Not sure about how expensive it is, but a milisecond slower webpanel is better than one that doesn't work at all. I just make sure I put my init-checks at the beginning of ajaxComplete

1 vote
Hans Pesata November 6, 2018
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context, reason) {
var $context = $(context);
switch(reason) {
case JIRA.CONTENT_ADDED_REASON.panelRefreshed:
{
$context.find('.element-in-plugin').each(function() {
// Your code
});
}
break;
default:
break;
}
});

Regards,
Hans 

0 votes
cdlee August 15, 2018

we have the same problem, did you figure it out?

Suggest an answer

Log in or Sign up to answer