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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Including js file from resources

drichter
15-Moonstone

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?

ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

9 REPLIES 9

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

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

drichter
15-Moonstone
(To:ClayHelberg)

Thx this works 🙂

 

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?

drichter
15-Moonstone
(To:drichter)

Ok I found a solution for this. Add this

 

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

in Application Styles

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

Hi Tomas--

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

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

--Clay

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

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(); 

 

Nice workaround.

Thank you much 🙂

 

Tomas

Announcements

Top Tags