"Export" a REST Resource JavaScript result


(Gabriele Antonini) #1

Matrix Version: 5.3.4.1
Hi, I’m wondering if there’s a way to make the output of a REST Resource Javascript Asset available in the Matrix context. I explain better:
Using REST Resource Javascript I’m loading a script file which create a javascript object with some properties. I’d like to have some of these properties available in other assets. I was thinking something like create a matrix global or session variable but I’m not sure it can be done from the javascript context.
Some code:
var response = _REST.responses[0].body;
eval(response); // this creates a “user” js object with name and surname properties
print(user.name); // this prints “Joe”

I’d like to make “Joe” available as a global variable so that I can use it anywhere in the site.
Is that possible?


Using response from one REST resource in another
(Bart Banda) #2

You could do that using the Call REST REsource trigger action. Anything you print(); within the JS area of the REST trigger action will be available to put in a session var using the Session Var Name field:

You could even set your “user” js object as the session var and then access any attribute from it from the session var value.

For example, if you use JSON.stringify() and print that value into a session var called “user”, the global session keyword to access that will be %globals_session_user%. This will print you the object as a string. To access attributes within that object, you can do things like:

%globals_session_user^json_decode^index:name%

read more about the index and json keyword modifiers here: https://matrix.squiz.net/manuals/keyword-replacements/chapters/keyword-modifiers#Available-Keyword-Modifiers

Specifically check out the json_decode modifier:


(Gabriele Antonini) #3

WOW, that sounds pretty cool, I’ll give it a go!
Thanks,
Gab