germainelol
germainelol

Reputation: 3331

Jade / JavaScript - Access data being passed to Jade

I'm passing a Jade template some data, which is in JSON form, so for example here me outputting the names of all the teams in the Jade template:

p Teams:
  br
  -for(var i = 0; i < tournamentData.teams.length; i++) {
    text Team #{i+1}: #{teams[i].name}
    br
  -}

This prints all the names of the teams fine. How would I access this data in a JavaScript file? Here I can simply type #{teams[0].name} to get the name of the team at 0, but how would I do this in the JavaScript file?

Upvotes: 0

Views: 592

Answers (1)

karaxuna
karaxuna

Reputation: 26930

If I properly understood you, you can do it like this:

JS file:

function myFn(teams){
    // user teams here
    // for example:

    window.myTeam = teams[0];
}

Including file in jade:

script(type="text/javascript", onload="myFn(#{JSON.stringify(tournamentData.teams)});", src="urlHere")

Update:

function myFn(bracketData){
    $(function() { $('#tournamentBrackets').bracket({ init: bracketData }); });
}

Upvotes: 2

Related Questions