3

I'm trying to get a .js file inside a zipped file loaded in Salesforce as Static Resource from the controller of an aura component. When I use:

baseUrl: "{!URLFOR($Resource.layer_composer,'js/lib')}",

I get the wrong URL like:

https://sit-sit-octoforce.cs86.force.com/businesscustomer/s/%7B!URLFOR($Resource.layer_composer,'js/lib')

(js/lib it's the folder I want to and layer_composer the static resource). Clearly it's wrong, so which is the right way to retrive js/lib inside the layer_composer Static Resources from the aura component controller?

9

See $Resource.

To use $Resource in JavaScript, you have to actually call a method. Here's an example from the docs:

({
    profileUrl: function(component) {
        var profUrl = $A.get('$Resource.SLDSv2') + '/assets/images/avatar1.jpg';
        alert("Profile URL: " + profUrl);
    }
})

When using URLFOR in component markup, you can use it directly, as demonstrated here:

<aura:component>
  <ltng:require 
    styles="{!$Resource.SLDSv2 + '/assets/styles/lightning-design-system-ltng.css'}"
    scripts="{!$Resource.jsLibraries + '/jsLibOne.js'}"
    afterScriptsLoaded="{!c.scriptsLoaded}" />
</aura:component>

If you need more than one resource, use join:

scripts="{!join(',', 
    $Resource.jsLibraries + '/jsLibOne.js', 
    $Resource.jsLibraries + '/jsLibTwo.js')}"

URLFOR is an artifact of Visualforce, and is not needed in Lightning at all.

  • with "$A.get('$Resource')" is taking a well formed URL but it's still the wrong URL. My Static Resource is in: "octoforce--sit.cs86.my.salesforce.com", while the alert give me "sit-sit-octoforce.cs86.force.com/businesscustomer/asset" where "businesscustom" is the community name. Is it means that I have to put the layer_comper zip file inside the community asset? – L.benedettini Jan 15 '17 at 10:07
  • @L.benedettini I don't have a lot of experience with community (about 5 minutes worth), but yes, it does appear that you need to re-upload the images as assets in the community asset resources for them to be available. Can you try that and let me know if it works? – sfdcfox Jan 15 '17 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.