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

前端 未结 5 852
天涯浪人
天涯浪人 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:06

    Scott's answer is the proper way to insert non-global JS into a specific view. Just a little comment, though: the block call from the view should not have the dash. It should be as follows:

    <% block('localScripts', '') %>

    Both calls will work, but using the dash makes the insertion twice; once the view is loaded and previous to the layout render, and then once again when the view is inserted in the rendered base layout. This leads not only to inserting/running unnecessarily twice the same code but also to errors that break your JS code if the inserted script depends on libraries that you have in your base layout (e.g. jQuery, Backbone).

    EJS interprets the magic <%- as "insert unescaped". So, -I guess- what this is doing is calling the block() function, which returns our HTML

提交回复
热议问题