cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Including js file from resources

SOLVED
Level 10

Including js file from resources

Hi,

 

I have a *.js file with functions that I will call from home.js. I uploaded the js file into the resources. How can I include this js file, so that I can call the functions?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Including js file from resources

There's a function for this:

 

$scope.app.fn.loadResourceScript("Uploaded/adapter-latest.js");

 

Just replace "adapter-latest.js" with your JS file name, and Bob's your uncle.

 

--Clay

9 REPLIES 9

Re: Including js file from resources

So i have not had time to try this yet but had the same question and someone suggested this code might work.

 

document.write('<script type="text/javascript">
console.log("In Prod Mode: Trying to load Pre-cache")
</script>');
document.write('<script src="script2.js"></script>'); //might need to make a better path to the js file like app/components/myJS.js

Re: Including js file from resources

There's a function for this:

 

$scope.app.fn.loadResourceScript("Uploaded/adapter-latest.js");

 

Just replace "adapter-latest.js" with your JS file name, and Bob's your uncle.

 

--Clay

Re: Including js file from resources

Thx this works Smiley Happy

 

Is there any way to include css files? loadResourceStyle will not work.^^

 

PS: I can not find a api, have someone a link to an api?

Re: Including js file from resources

Ok I found a solution for this. Add this

 

@import url(#{$resources}/Uploaded/test.css);

in Application Styles

Re: Including js file from resources

Hello  ClayHelberg,

 

your syntax works realy good. In .JS file I can use every imported function.

Screen_253.bmp

My question:

Is there any way how to call imported function directly from widget script?

Common syntax does not work:

Screen_254.png

Thank you for answer.

 

Tomas

Re: Including js file from resources

Hi Tomas--

Remember, in widget scripts, the $scope is implied. So, try something like this:

app.fn.loadResourceScript('Uploaded/Buttons/andCheckBoxSwitch.js');

--Clay

Re: Including js file from resources

This is good to know. But I meant it a little differently.
I have imported library with functions in Home.JS.

I can use these imported functions in Home.JS without any problems.
But if I want to use these functions inside the widget, nothing happens.

 

Have you ever met this problem?

 

Tomas

Re: Including js file from resources

Ah, I see now. The key to this will be to get your JS objects defined in the library into the angular $scope. So, if you have a top level object that contains your functions, you could just add that to the scope:

 

$scope.myLib = myLib;

and then in your widget JS box you can use

myLib.myFunc(param1, param2);

If your library is just a series of function definitions, then you'll likely need a wrapper for each one. If you have something like this:

 

 

function jsTest() {
  alert("jsTest successful!");
}

then add this wrapper function to your Home.js:

$scope.jsTest = function() {
  jsTest();
}

and then in your widget JS you can call it in the usual way:

jsTest(); 

 

Re: Including js file from resources

Nice workaround.

Thank you much Smiley Happy

 

Tomas