Logic against keyword in REST Resource JavaScript asset not working


(Birdg) #1

Hi,

 

We are on 5.0.2. We are using a REST Resource JavaScript asset to display some data from a JSON API along with metadata from an asset. In the textarea on the Details screen of the asset, we have some basic JS logic as follows:

 

var aleveloffer = "%globals_asset_metadata_ugcourse.aleveloffer%";
 
print(aleveloffer); // prints aaabc
 
if (aleveloffer == "aaabc") {
  print("Has a level offer") ;
} else {
  print("No a level offer");
}
 
However, although the variable is printed, the JS logic fails. Does anyone know why this might be?
 
Thanks,
 
Graham

(Nic Hubbard) #2

Are you sure that there are no spaces in the string?

 

Could you try something else just to make sure other js logic is working? E.g.:

var a = 1;
if (a == 1) {
   print('1!!');
} else {
   print('Something else!!');
}

(Hopkinsd1) #3

Hi Nic,

 

I can confirm that there are no spaces in the string.

 

I have added the JS that you suggested and it prints '1!!' to the page. So the logic is working correctly.

 

It appears as if it cannot do logic when testing against a Matrix global keyword.

 

Thanks

David


(Joel Porgand) #4

 

It appears as if it cannot do logic when testing against a Matrix global keyword.

 

 

What I think might be happening is the keywords are only processed at the end - so you're actually printing the string "%globals_asset_metadata_ugcourse.aleveloffer%" which will then be processed by matrix before you see the final output (aaabc). When you're applying the logical test it's still "%globals_asset_metadata_ugcourse.aleveloffer%". 

 

Do you have "Pre-Process Matrix Global Keywords" set to no? If so try turning it on and see how you go. 


(Hopkinsd1) #5

Hi JP,

 

Thanks for the reply.

 

We do have "Pre-process Matrix Global Keywords" set to Yes and it doesn't make any difference. 

 

The thing that confuses me is that when trying this 

var aleveloffer = "%globals_asset_metadata_ugcourse.aleveloffer%";
print(aleveloffer); // prints aaabc

print(aleveloffer) outputs the value of the metadata.

 

But when we check the value of "aleveloffer" within an IF statement is doesn't appear to hold the value.

 

Thanks


(Joel Porgand) #6

I think that's because when you're making the test you're effectively doing this:

 

if ("%globals_asset_metadata_ugcourse.aleveloffer%" == "aaabc") {

 

Which would evaluate false at the time of the test. It's only later after all the javascript has run that the keyword is processed into a value by matrix. 

 

As a bit of a hack you could try passing the metadata value as a parameter in the URL of the REST asset you should be able to retrieve it again from the _REST object. Not entirely sure that'll work but maybe?