Miroslav Popov
Miroslav Popov

Reputation: 3383

Pass Jade model variable to a view script function parameter

I want to pass a Jade model variable as a parameter to a JavaScript function.

I'm reading the socketUrl variable from config and passing it to the view:

app.get("/", function (req, res) {
    var socketUrl = config.get("socketUrl");
    res.render("index", {title: "Ticker", socketUrl: socketUrl});
});

Now I wont to use it in a view script ticker.initialize(socketUrl):

html
  head
    script(src="js/socket.io-1.4.5.js")
    script(src="js/ticker.js")
    script.
        document.addEventListener("DOMContentLoaded", function () {
            ticker.initialize(socketUrl);
        });

The browser cannot read socketUrl in that way. How to pass it correctly?

Upvotes: 1

Views: 282

Answers (1)

michelem
michelem

Reputation: 14590

This should work but only with strings:

script.
    document.addEventListener("DOMContentLoaded", function () {
        ticker.initialize("#{socketUrl}");
    });

If it's an object try !{socketUrl}

Upvotes: 1

Related Questions