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

connect add-on and resources

Frédéric Tardieu
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.
October 6, 2015

Hi all,

How to access a resource (I mean: how to build a path in Javascript to a file or a directory located in the hosted connect add-on)?

AJS.params.baseURL is defined, should it be used for this purpose? if yes, what should be added to AJS.params.baseURL to build the complete path to a resource?

Thanks a lot,

Fred

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
i1
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 6, 2015

Hi Fred,

 

Thank you for asking. It is not clear to me what resource what are we talking about here. Here are some options:

  • If you are trying to access resource that is part of your addon - then. if it is accessible via http - that's what you can use (Browser, AJAX request, curl, etc to get a resource hosted on your addon server). For example, following snippet of JS code, when loaded as past of your addon page will request a resource from the same host where the page was loaded from:

    var pathToResource = "/path/to/resource/on/your/host";
    $.get( pathToResource, function( data ) {
      // DO something with the resource
      alert( "Load was performed." );
    });
  • However if you want to get a resource from host application that calls your addon (from an iframe) you can use request module: https://developer.atlassian.com/static/connect/docs/latest/javascript/module-request.html

    // Display an alert box with a list of JIRA dashboards using the JIRA REST API.
    AP.require('request', function(request){
      request({
        url: '../assets/js/rest-example.json',
        success: function(responseText){
          alert(responseText);
        }
      });
    });

    This snipped when run from your addon page will request a resource  '../assets/js/rest-example.json' from host JIRA and will display its content in an alert.

Does it answer your questions? If not could you please provide a bit more details about what are you trying to do, such as: 

  • Is resource hosted by addon server or by JIRA?
  • What is the example use case for this resource?

 

Hope that helped.

Ivan

Frédéric Tardieu
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.
October 6, 2015

Hi Ivan, Thanks a lot for your reply! Let me clarify the use case: it's about dynamically loading Javascript files thanks to a Javascript AMD-like loader. - the javascript files to load are hosted by the add-on, in public/js/lib directory - the javascript loader file is also hosted by the add-on, in public/js What I need to do, for instance in addon.js, is something like this: myLoader.baseUrl = 'public/js/lib'; myLoader(['firstScriptToLoad.js', 'secondScriptToLoadWhichWillLoadAllTheOtherScriptsNeeded'], function () { // scripts loaded, I do the job }); My problem is to construct the path (myLoader.baseUrl) which will be used by the loader to find the Javascript files to load. And, generally speaking, to be able to construct the path to any file hosted by the add-on, Hope it's more clear! Thanks a lot again, Fred

i1
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 7, 2015

Ok, so it sounds to me that you want to access resources on your own addon server. And it seams that you would like to use AMD loader for that. There are a number of existing solutions for it as it is not a part of connect. One such library would be requirejs: http://requirejs.org/docs/start.html but many other exists. I am not sure what is the problem with constructing the baseUrl thou - since resources are loaded from the same host as the addon page - that is from the hosting where addon is hosted - using something like: '/public/js/lib' - is sufficient. Or, using requirejs as an example: requirejs(["public/js/my_module"], function(util) { // Do something }); Note in this example 'public/js/my_module' is a WELL KNOW name of the resource on your host. That is when you created this resource - you gave it a name and thus know this name and location where this resource is located on the host. Please see documentation for the AMD module of your choosing for details. Hope that helped, Ivan

TAGS
AUG Leaders

Atlassian Community Events