How to load resources in link dialog vm

I have made velocity file which is being used in link dialog. But when I am trying to load the resources using

$webResourceManager.requireResource("com.jira.app.test:link.screen.resources")
or
$webResourceManager.requireResourcesForContext("test.linkScreen")

My resource file defined in web resource are not being loaded.
Here is my atlassian-plugin.xml

 <!--Link screen-->
    <web-item key="test.link" section="create-issue-link-types" weight="110">
        <label key="test.link.webItem.label"/>
        <link linkId="add-test-link">
            /secure/ActionTestLink!default.jspa?id=${issueId}
        </link>
    </web-item>

    <webwork1 key="test.link2" name="Link Test" class="java.lang.Object">
        <actions>
            <action name="com.jira.app.action.ActionTestLink" alias="ActionTestLink">
                <view name="success">vm/linkTest.vm</view>
                <view name="error">vm/linkTest.vm</view>
            </action>
        </actions>
    </webwork1>

    <web-resource key="link.screen.resources" name="test Link Resources">
        <dependency>com.atlassian.auiplugin:ajs</dependency>
        <resource type="download" name="linkTest.js" location="js/linkTest.js"/>
        <resource type="download" name="linkTest.css" location="css/linkTest.css"/>
        <context>test.linkScreen</context>
    </web-resource>

Location -

Edit:
For people I explicity invited can you answer or invite people who knows this kind of stuff.

Hi Saurabh. Here are a few things to consider and/or try:

a) Check the <head> of the HTML page that you’re looking at. Does it include elements related to the download of linkTest.js and/or linkTest.css. If it does, try using the URLs in your browser window; are they correct? If those elements are not in the <HEAD>, then we know that the web resources are not being included for some reason.

b) Is it possible that $webResourceManager is not available in the Velocity context for the link dialog? Maybe try printing $webResourceManager.ClassName in the body of the Velocity template. If it’s not in the Velocity context, you could have your webwork action put it there explicitly.

c) The key to your web resource is link.screen.resources. This is a shot in the dark, but I recall having a problem with a web panel that had a similar key, and I solved that problem by replacing the dots with dashes: so link-screen-resources in your case.

Let us know what you find out.

David

1 Like

So the good news - you’re not doing anything wrong… The problem is that the contents of the dialog in that location is dynamic so webresourcemanager isn’t being called to get the tags.

The fix though is pretty simple.

In your ActionTestLink add:


    @HtmlSafe
    public String getScriptTags()
    {
        return this.webResourceManager.getResourceTags("com.jira.app.test:link.screen.resources", com.atlassian.plugin.webresource.UrlMode.ABSOLUTE);

    }
}

Make sure to inject WebResourceManager into the class though - if you’re using SpringScanner it would be:

    @ComponentImport
    private final WebResourceManager webResourceManager;

    @Inject
    public ActionTestLink(final WebResourceManager webResourceManager)
    {
        this.webResourceManager = webResourceManager;
    }

Then in your vm file, just add: $action.getScriptTags(). I should note that you’ll need to add this in the body (if you’re using all of the <html><head>...</head> stuff since everything outside of the <body></body> is removed.

That’s it - the script tag will be put in place when the page loads. You might want to remove the dependency on auiplugin:ajs since it’s already on the page at that point.

This seems to work for me in Chrome and a couple of other browsers.

If you really want to do it cleanly, you could add some javascript to the atl.general context which listens for this particular dialog to load which then loads the extra javascript resources at that point through a rest end point.

2 Likes

Worked perfectly. Thank You