Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

initFancyPlugin() function call at AJS.toInit() is not defined - Loading Javascript (web-resources) doesn't work since upgrade from JIRA 5.0 to 5.1

Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2012

//edit:
please see comments for further problems development. The original thread is just for completeness of content.


Hello there,

I have a big problem and looked for answers the last three whole days.

I am developing a plugin for my company, which runs JIRA 5.0.2 but plans to update to 5.1+ soon. So I tested my plugin on my local developer instance using atlassian-plugin-sdk v4.0, which works flawless under 5.0.2, to work with 5.1.8, but it failed.

I cannot use any of my .js files anymore. I need to change accessing/using my resources, but I cannot figure out how to do it right.

For Details (that's how I did it until now):

atlassian-plugin.xml

...
<web-resource key="my-fancy-resources" name="Fancy Resources">
	<resource type="download" name="fancy-common.js" location="includes/js/fancy-common.js"/>
	<resource type="download" name="fancy-execute.js" location="includes/js/fancy-execute.js"/>		
	<resource type="download" name="fancy-style.css" location="includes/css/fancy-style.css"/>
        <context>my.fancy.plugins.fancy-context</context>
</web-resource>
...

FancyPlugin.java

...
WebResourceUrlProvider webResourceUrlProvider = ComponentManager.getComponent(WebResourceUrlProvider.class);

Collection<String> cssUrls = new LinkedList<String>();
cssUrls.add(webResourceUrlProvider.getStaticPluginResourceUrl("my.fancy.plugins.fancyplugin:my-fancy-resources", "fancy-style.css", UrlMode.RELATIVE));
velocityParams.put("cssUrls", cssUrls);
	        
Collection<String> jsUrls = new LinkedList<String>();
jsUrls.add(webResourceUrlProvider.getStaticPluginResourceUrl("my.fancy.plugins.fancyplugin:my-fancy-resources", "fancy-common.js", UrlMode.RELATIVE));
jsUrls.add(webResourceUrlProvider.getStaticPluginResourceUrl("my.fancy.plugins.fancyplugin:my-fancy-resources", "fancy-execute.js", UrlMode.RELATIVE));
velocityParams.put("jsUrls", jsUrls);
...

fancyplugin.vm

#foreach($css in $cssUrls)
	<link rel="stylesheet" type="text/css" href="$css" />
#end
#foreach($js in $jsUrls)
	<script language="Javscript" type="text/javascript" src="$js"></script>
#end
...

//edit (thanks to conro):
I added <context>atl.general</context> and it worked! But I want my js only be loaded on my plugins tab panel.
"Reducing" the context to new jira.browse.projects didn't work at all - none of my web-resources were added (checked with firebug console).

As I read about problems with loading resources, I came across the velocitiy macro #requireResource("..."). But when ever I use this it just prints this macro as String on to the page and does not load the js-files.

So I imported the WebResourceManager to my java. And called requireResource() (and tried requireResourceForContext() too) an I could see in Firebug that it got the resources via GET. But it did not "inject" them (don't know how to say it), because Firebug keeps printing error messages of all my javascript functions cannot be loaded. The same happens when I put webResourceManager to my velocityParams and call $webResourceManager.requireResource() within my velocity template.

I'm kind of despaired...

I hope someone can help me with that, because my company really plans to update JIRA, which would make my plugin impossible to use.

Thanks in advance.

Best regards,

Carsten A.

8 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2012

I was able to do it finally:

AJS.$(document).ready(function () {
	JIRA.Project.navigationTabs.addLoadEvent('com.myfancyplugin.tab-panel', initFancyPlugin);
});

pretty much did the job staying with (my good old) WebResourceUrlProvider. I recognised too, that all "inline" events (such as onclick and onchange) in my velocity template resulted in error (... is not defined) in JIRA 5.1 (while it worked well in 5.0 and less). Removing all that inline events and adding

jQuery('#myDomElement').click( function() { callToMyOnClickFunction(); /*and more*/ });

in initFancyPlugin() solved that as well.


Doing it that way, works in JIRA 5.0 as well as in 5.1 (5.2 will be tested tomorrow, but I'm confident).

And thank you conro for your attempt.

1 vote
ConradR
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 25, 2012

Did you also call requireResourcesForContext() from your Velocity Template? This should be the right way.

If this doesn't work, you should create an issue: https://jira.atlassian.com/secure/Dashboard.jspa

$webResourceManager.requireResourcesForContext("my.fancy.plugins.fancy-context")

Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 25, 2012

To clearify usage:
I create an instance of WebResourceManager class and pass it as parameter to velocity. From the template I call requireResourcesForContext() using the passed parameter. Am I right?

ConradR
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 25, 2012

$webResourceManager should be available in your Velocity Template already. Try not passing it from your Java Code and just call the following line from the template:

$webResourceManager.requireResourcesForContext("my.fancy.plugins.fancy-context")

(Maybe the WebResourceManager you have created is null for some reason and you override the one in your template..)

btw: I didn't read your code. You don't have to include your files by hand. You can delete the whole code of FancyPlugin.java and fancyplugin.vm you have posted here. You just need the definition in atlassian-plugin.xml and the call of the webResourceManager!

Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2012

Sorry for the delay...

First of all, thank you.

I have done pretty much debugging and a lot of tries. I commented the original thread with the furhter problem development.

Maybe you could help me with that?

0 votes
Keith Ballantyne October 20, 2015

where did you put?

AJS.$(document).ready(function () {
    JIRA.Project.navigationTabs.addLoadEvent('com.myfancyplugin.tab-panel', initFancyPlugin);
});

I cannot consistently get ready() to be called.  I assume this (or registering for the event using bind) must happen in "other" javascript, rather than the script that is loaded with the page.

0 votes
Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2012

just for completeness, this is what I've tried so far:

(function($) {  // I did try commenting this line out to just have AJS.toInit(), too.
	AJS.toInit(function(){
		initFancyPlugin();
	});
})(AJS.$);  // (see above comment)

and:

AJS.$(window).load(function () {
	initFancyPlugin();
});

and:

AJS.$(document).ready(function () {
	initFancyPlugin();
});

and just only initFancyPlugin();

All that inside my fancy-execute.js (where it seems to be just ignored completely) or in a script tag inside my velocity template (where I get "initFancyPlugin() is not defined" error if any).

0 votes
Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2012

I did. I would have to add com.pyxis.greenhopper.jira:gh-common, which would load 21 resources and 6 dependencies itself. As I said I only need one js. But that's not the problem. I could live with that.

Despite all that I still am unable to call initFancyPlugin() automatically when the project tab is loaded. Either all function calls are ignored without any warning or error, or it tells me that my function "is not defined". All worked with JIRA 5.0 and less!!!

0 votes
ConradR
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2012

If you have a dependency, you should add it to your web-resource:

<dependency>com.atlassian.auiplugin:aui-experimental-restfultable</dependency>

0 votes
Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2012
Thanks to conro, I could include all my ressources the way you suggested. But after that two new problems appeared
  1. I have a dependency where I need a single js-file out of Greenhopper. Doing it via requireResourceForContext() doesn't allow me to load a single resource out of a web-resource-package of a different plugin. So I would get a lot of "junk" resources. But I could live with that, if there wasn't:
  2. I have a javascript function which initiates my plugin: initFancyPlugin() (which also results in some visual enhancements - so I can just see if its loaded or not)
    I am not able to run it. If I just write the call into my js resource, it is kind of ignored (no visual inhancements an no errors on firebugs console.log or the system.out). It's the same, when I write it into script tags within my velocity template. It's even the same adding it to AJS.toInit().

I tried going back to WebResourceUrlProvider and suddenly it (kind of) worked. (Don't know why it suddenly loads - and days ago it didn't.) I could see in firebugs console.out that the resources were loaded, BUT: At no time I was able to successfully call initFancyPlugin(). When I write the function call into my js resource, it's ingored like in previous tries. When I put it in script tags inside my velocity template AND in AJS.toInit() firebugs console.log just shows the error initFancyPlugin() is not defined right after it tells me that all resources have been loaded - even if I use setTimeout() its still "not defined".

I am at my wits' end...

0 votes
ConradR
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2012

Does the web resource work with <context>atl.general</context> ?

Carsten A.
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2012

Yes, it does.

But I dont want my js loaded/available outsite the tab panel.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events