Sails.js - How to inject a js file to a specific route?

前端 未结 5 855
天涯浪人
天涯浪人 2020-12-13 07:39

For example, I have a page /locations/map which I need to include Google Map library, and include a .js file (e.g. location.js) specif

5条回答
  •  既然无缘
    2020-12-13 08:13

    I know this is an old question by I discovered another option.

    File:

    config\routes.js

    Code:

    '/': 'HomeController.StartHome'
    

    I set StartHome function in HomeController responsible for managing '/' route.

    File:

    api\controllers\HomeController.js

    Code:

    StartHome: function(req, res) {
    
        var template_data = {
            "data" : {
                "view_name" : "home_view"
            }
    
        }
    
        res.view('home_view', template_data)
    }
    

    Here, I created an object with some data which is passed to EJS template(to client).

    File:

    views\layout.ejs

    Code:

    <% if (data["view_name"] === "home_view") { %>
    
    <% } %>
    

    In my layouts.ejs I created if statement which "enables" script tag depending on on the view I am currently on.

    This is how I handle this. I hope it's clear for you.

    Sails 0.12.4

提交回复
热议问题