Jump to content

Reading an external JavaScript file


EricC

Recommended Posts

I am setting up a store with 300 templates.

 

All 300 templates use the SAME FusionPro rules.

It's a painful process whenever a rule has to change - I have to modify and then re-upload all 300 templates!

 

When using FusionPro on my desktop (when building the template in Acrobat Pro), I use an OnJobStart rule to load a file named 'rules.js'

 

The 'rules.js' file contains all of my FusionPro rules.

 

At the desktop, when I test/preview my template, it works perfectly!

This is great stuff!

 

So, I log into the Manager and I upload my template.

After uploading the template, I click on the 'External Data Files' link, and upload the 'rules.js' file.

 

Next, I 'link' that external data file to my FusionPro template.

 

I go back to my template (in the Manager) and click the 'Preview' button.

I generate a proof and it works!

(In other words, this FusionPro template (inside the printable Manager) is reading the external file)

 

So far so good.

The last step is to go to the Configuration tab and click the 'Publish...' button.

 

However ...

When I log into my printable store, click on that item and generate a proof, the rules do not work.

 

Why does the external data file work in the Manager, but stops working after you publish it?

 

I know that the 'External Data File' feature in the Manager is meant to be used for a tab-delimited data file (a flat file containing actual records). But I didn't think there was any reason why it couldn't ALSO be used to load JavaScript rules.

 

So, if there is no way to get my FusionPro template to read an external data file using the 'External Data File' feature in the Manager, is there a way to get it to load an EXTERNALLY HOSTED file?

 

In other words ... similar to how a CDN (Content Delivery Network) hosts jQuery code or CSS (style sheets) ... is there a way to add a rule to my FusionPro template that says:

 

'On Job Start, load this JavaScript file which is hosted at http://www.abc.com/scripts/rules.js'

 

The whole purpose of this is so that I can have all of my FusionPro rules in one file. Any of my templates should be able to "call" it. And the benefit is that I can update one single file instead of updating (and reuploading) 300 fusionpro templates.

Link to comment
Share on other sites

You can use the Load function to import an external .js file, or a set of files. For example:

Load("rules.js");

 

However, it only accepts a file path, not a URL. For MarcomCentral, you should be able to upload the .js file as a resource and then Load it. I think you can add it to a library as well, and then use it in any job.

Link to comment
Share on other sites

Hi Dan,

 

you should be able to upload the .js file as a resource and then Load it.

 

Do you mean, include it in my 'collect' folder and upload it along with the template? The upload button in the online template manager automatically filters out certain file extensions. So in other words, when I click the 'browse' button and then navigate to my collect folder, the rules.js file does not appear.

 

I can get around that by simply renaming 'rules.js' to 'rules.txt'. (and of course I have to change the name of the file I am loading within my FusionPro template). This actually works, but ONLY while in the Manager. As soon as I click the 'Publish' button (to publish the template online), it stops working! (Meaning, if I log into the site as the customer and test the template, the rules don't work any more)

 

 

--

I think you can add it to a library as well, and then use it in any job.

 

This sounds promising, but I'm not sure how to upload a JavaScript file to a Library. From what I can tell, there are Images, Colors, Text Lists and Image Collections. Of those, the only one that lets you actually upload anything is the Images Library, but it only allows you to upload images (tiff, eps, pdf, etc)

Link to comment
Share on other sites

This sounds promising, but I'm not sure how to upload a JavaScript file to a Library. From what I can tell, there are Images, Colors, Text Lists and Image Collections. Of those, the only one that lets you actually upload anything is the Images Library, but it only allows you to upload images (tiff, eps, pdf, etc)

 

I got this work by uploading the javascript file to External Data Files, then linking it to the template at the bottom of the Configuration tab. You need to also put a bit of code in that tells MC to turn on that fuctionality.

 

OnJobStart

XJS = new ExternalDataFileEx("sample.js", ",");

 

OnRecordStart

Load("sample.js");

 

I'm not positive about the OnJobStart rule and how MC enables use of External Data Files. It may just be that you have to use that function somewhere to turn it on. Let us know if you get it working or find a better way to do it.

Link to comment
Share on other sites

  • 2 months later...
I got this work by uploading the javascript file to External Data Files, then linking it to the template at the bottom of the Configuration tab. You need to also put a bit of code in that tells MC to turn on that fuctionality.

 

OnJobStart

XJS = new ExternalDataFileEx("sample.js", ",");

 

OnRecordStart

Load("sample.js");

 

I'm not positive about the OnJobStart rule and how MC enables use of External Data Files. It may just be that you have to use that function somewhere to turn it on. Let us know if you get it working or find a better way to do it.

I don't see how just calling ExternalDataFileEx would make any difference to a subsequent Load call. In any case, you probably don't want to be re-loading the .js file for every record. If what you have above works, with the file uploaded to MarcomCentral, then it should work to simply put the Load call in OnJobStart, without the ExternalDataFileEx call.

 

Again, though, someone on the MarcomCentral side of things will have to answer specific questions about how to upload the files there.

Link to comment
Share on other sites

Hi everyone,

When I posted this original question here, I was hell-bent on getting it to work.

 

As Dan confirmed, the 'Load' function only accepts a file path, not a URL. So having an externally hosted javascript file won't work.

 

If I rename my .js file as .txt and then call it in my template, it works. But again... as soon as I click 'Publish' it stops working.

 

I haven't tried this since September, but somewhere in this thread ThomasLewis stated he got it to work.

 

This account is starting to pick up again for me so maybe I will give it another shot.

 

Have you tried it?

Link to comment
Share on other sites

Working on my end in Marcom Central. No issues that I can see. Loaded the .js file to the external data files library, made sure to map it in the configuration settings of the template, and tested the template with the javascript file mapped and unmapped to make certain it was not catching any legacy settings. Pretty cool to be able to load the bulk of my rules in case I need to make a some quick changes and not have to republish every time...Thanks PTI...
Link to comment
Share on other sites

It works for you?

If you don't mind can you give me some details?

 

How do you reference the file?

 

How did you upload the file in the Manager? (as a Text Library or as en External Data File)

 

Did the file have to end in .txt?

 

Are your javascript rules arranged in the text file in some sort of tab- or comma-delimited fashion? or is it just a text file with a bunch of rules?

Link to comment
Share on other sites

To be clear I have a bunch of functions in the .js file that are returned in a series of rules in the template.

 

1. load the file using the OnJobStart in the FP template.

//OnJobStart
Load("Globals.js")

 

2. I used the MC External Data Files library to upload. I loaded the .js file as is no changing the extension.

 

3. Make sure you map the external data file in the configurations file tab in the Manager template.

 

 

Make sense?

Link to comment
Share on other sites

3. Make sure you map the external data file in the configurations file tab in the Manager template.

 

That's the part that doesn't work for me unless ExternalDataFileEx() is called in OnJobStart. Only then do I get the option to choose the txt/js file.

 

If you don't make a call to that function MarcomCentral will give you this message:

* The Template does not use External Data Files functionality.

 

and you don't get the field where you can choose the file.

Link to comment
Share on other sites

Figured out why it does not work for you all......I have two external files I am loading. One is a tab delimited file that looks up sku numbers and the other is the actual .js script file. I am mapping the first file because I am using ExternalDataFileEx() but the second file is a raw .js script and therfore cannot be read by itself...meaning you need to have an actual ExternalDataFileEx() function or object in a callback rule. The trick here is that since i am using an actual database file the rest of the External Data Files library becomes visible and therefore can be mapped. This is simply pointing the template to the .js file in the same manner the FP Creator would if you had the .js in the same directory as the template. Make sense?
Link to comment
Share on other sites

In theory one could have a dummy ExternalDataFileEx() in a callback rule and that would do the trick. Do not know if PTI would consider this a messy hack since I have never seen/heard of it being done until now.....Anyone from PTI care to chime in before a couple of us start using this method?????
Link to comment
Share on other sites

You can call the ExternalDataFileEx() and comment it out. Marcom looks for the ExternalDataFileEx() and enables the External Data File area in the configuration tab in the manager when found. Once you upload a template that contains ExternalDataFileEx() (even if commented out) you can associate your external .js file to the product. Edited by stellasparkles
Link to comment
Share on other sites

You can call the ExternalDataFileEx() and comment it out. Marcom looks for that function in order to enable the External Data File area in the configuration tab in the manager. Once you upload a template that contains this function (even if commented out) you can associate your external .js file to the product.

 

Nice. So I assume this is allowed since the External Data Files library allows us to upload javascript files?

Link to comment
Share on other sites

I don't see how just calling ExternalDataFileEx would make any difference to a subsequent Load call. In any case, you probably don't want to be re-loading the .js file for every record.

 

It's been awhile since I messed with this but I believe that what I was running into is anything in the .js file that was defined as a var wasn't working outside the callback unless you defined it globally. Since 99% of all my JavaScript is done in OnRecordStart I was able to bring the file in there and not have to worry about variables not working. Also, I was doing this for a versioned product in MarcomCentral so there is only 1 record anyways.

Link to comment
Share on other sites

×
×
  • Create New...