SSJS and Asset Listings


(Lewis) #1

I’m nesting an asset listing in the Default Result Bodycopy of a Decision Tree but the SSJS the asset listing uses seems to be being ignored altogether. For the moment I’m simply trying to get it to print “Hello, world!” on the screen when the Decision Tree shows the content of the Default Result Bodycopy but it isn’t even doing that (see below).

<script type="text/javascript">

    console.log("Hello, world!");
    
    if(responses[%ds__qid%]) {
        if(responses[%ds__qid%].ans == "_no") {
            print("<p>%ds__content%</p>");
        } else {
            print("Something went wrong");
        }
    } else {
        print("Something went wrong");
    }
    
</script>

Any ideas?


#2

SSJS requires the attribute runat="server" to be included. And, you don’t need to specify the mime type.
Console will only work on the frontend.


(Lewis) #3

Hi @np81. Thanks for the reply!

I’ve tried runat=“server” but no luck unfortunately.

I took another look again yesterday and I think that the problem is the global variable I’m declaring in the Standard Page asset (where the Decision Tree is nested) isn’t shared with the Asset Listing I’ve nested in its Default Result Bodycopy.

I’m trying to find a way to do this so that I can group and categorise the suggestions to the user’s answers in the Decision Tree and present them at the end of the questionnaire. Unfortunately, there aren’t really enough keyword replacements in Squiz for Decision Trees to allow me to test each answer and respond appropriately so I’ve created this SSJS which accumulates the responses and then fetches them back from the global variable ( var responses = {} ), comparing the asset IDs stored in there to CSV data records imported from a list of possible suggestions.

I’m so close to a solution but the two ends aren’t talking to one another - the global variable is ready to go, full of user’s responses and the Asset Listing is working, rattling off the CSV data records ready to compare (and filter) but because the global JS (or SSJS) variable isn’t passed through to the Asset Listing, the Asset Listing doesn’t have a list to filter from.


(Lewis) #4

Weirdly, you can share JS variables in the opposite direction - from a nested asset (I just tried with the Decision Tree nested in the Standard Page) and it works - “Hello, world!”.

I’m looking at a way to move all the JS from the Standard Page (where it gathers the responses to the Decision Tree) to the nested Decision Tree itself at moment. Maybe that will work @np81


(Lewis) #5

I’ve now tried adding all the JS to the Design File for this web page and testing using a dummy variable - can I see the value of that variable from within the Asset Listing? No, it’s not available despite being available to the browser, as shown in developer tools in Chrome (I’ve added a Watch to the dummy ‘test’ variable and its value appears in dev tools).

This is really weird - a global JS variable, set in the Design File, isn’t available to the Default Format of an Asset Listing deeper in the site.

I don’t think SSJS is an option in this case because the JS itself needs to be able to access the DOM.


(John gill) #6

I find the best way to investigate these things is to view the page with the ?SQ_VIEW_SERVER_JS parameter.

That will spit out all the SSJS that would normally be run for the page, so you can check that it’s all accounted for and all in the right order. By the sounds of it, you might find the code is not running in the order you expect.


(Lewis) #7

Hi @JohnGill. Thanks for the reply!

I’ve moved all the JS into the Page Contents of the Decision Tree and I’m now looking into using JS’s MutationObserver to watch for the Default Result Bodycopy to appear. When it does, I’m planning to try and rattle off the suggestions (from the CSV data sources), filtering them against each of the answers (captured using JS from the Decision Tree).

I did use ?SQ_VIEW_SERVER_JS to look at the output but I didn’t see anything amiss :thinking: