Global keyword replacements in JS file


(Bmasters) #1

I'm hoping someone can help me out here.  
I've got a Standard Page asset called 'RHS' which is going to be nested into a whole bunch of other Standard Page assets (for example 'Scholarships', 'Exams' etc).
The RHS asset will reference a JS File called appConfig.js, which defines some variables.

Within appConfig.js I would like to reference metadata fields from my Scholarships/Exams pages

sessionscope: {taxonomyId:'%globals_asset_metadata_XXX%'} 

Is this possible?

At the moment, the keyword replacement is giving me the metadata field from appConfig.js

 

For context, the contents of appConfig.js must be a javascript file, and cannot be inline within RHS. If this were not the case, I'd be laughing, as the above code would work.

 

Any ideas appreciated


(Nic Hubbard) #2

If you are using a JS File asset this won't work. However, I have done something like this many times, and I used a Standard Page so that keyword replacements would work. Then I applied a Design to it that has the following:

<MySource_PRINT id_name="__global__" var="content_type" content_type="application/javascript" />   
<MySource_area id_name="body" design_area="body" />

Using this method you are able to make the Standard Page perform like a JS file, but have the advantage of keyword replacements.


(Bart Banda) #3

The best practice for this is to actually write the global keyword into a data attribute on the page somewhere on the front end, and then grab it with JS.

Something like this:

 

<div id="foo" data-val="%globals_asset_metadata_XXX%"></div>

 

<script>

  sessionscope: {taxonomyId:$('#foo').data('val')}  

</script>


(Bmasters) #4

Thanks the responses Nic & Bart. Both options work a treat!


(Tim Davison) #5

Just curious Nic, how does that approach go with browser caching?  Is Matrix smart enough to return a 304-Not Modified header?  I'm curious as that approach would be really handy in quite a few cases.


(Nic Hubbard) #6

Just curious Nic, how does that approach go with browser caching?  Is Matrix smart enough to return a 304-Not Modified header?  I'm curious as that approach would be really handy in quite a few cases.

 

No, I don't think it does. Just tested it on an old implementation of this. I used it mostly for internal sites.