4

I would like to insert a tag to a specific script into a specific view and nowhere else. This question very nearly captures my problem, but I want to use a script that I have written and wish to put into my public directory on the client side, rather than implementing a script tag with an hfref to an http address elsewhere. I see how the given solution allows you to put a script tag with a unique href in a a specific view, but I don't know where to store the js that I want to insert into the view so that it will be an available asset. I thought at first that any directory I placed inside of '/assets' would be placed inside the public directory on the client-side, but when I tried adding a '/assets/localScripts' directory and placing my script inside it, neither the directory, nor the script showed up. So I thought I'd put the local script inside the 'assets/js' directory, but everything I put in there (even if it's in a sub-directory) is getting added to every view and I don't want this script to be injected into the homepage (or anywhere accept one specific view).

I looked at the pipeline.js file in an attempt to modify the grunt tasks. This resolved question on excluding files from grunt tasks suggests that I should be able to tell grunt to ignore the file '/assets/js/localScripts/gameview.js', but I have had no luck. I tried adding to the end of the part of pipeline that selects js files to inject each of the following exceptions (to do the same thing):

var jsFilesToInject = [

  // Load sails.io before everything else
  'js/dependencies/sails.io.js',

   // Dependencies like jQuery, or Angular are brought in here
  'js/dependencies/**/*.js',

  // All of the rest of your client-side js files
  // will be injected here in no particular order.
  'js/**/*.js',

  //My attempted exceptions:

  //I think this should ignore anything in the localScripts directory
  !'**/localScripts/**',

  //I think this should ignore any file named gameview.js
  !'**/gameview.js',

  //I think this should ignore '/assets/js/localScripts/gameview.js'
  !'/assets/js/localScripts/gameview.js'
];

None of these exceptions worked (the script was injected into every page in each case). Am I on the right track? How can I make a js file available to views that have a script tag injected for it, but abstain from automatically injecting every view with a script tag for the file?

Community
  • 1
  • 1
aleph_one
  • 80
  • 10

1 Answers1

1

Your going to have to change your grunt file i think. Or . ..

My simple solution, what I use on my apps, is I get rid of the code in the layout file that has sails inject scripts automatically. I place my scripts in the assets/js directory (and sub directories). Then I just reference them directly in my files.

If I want them in all my views, then I add it to my layout file, if I want it only on a specific page then I put it in that file.

Meeker
  • 5,979
  • 2
  • 20
  • 38
  • This is exactly what I wanted! To clarify: the code that needs to be deleted in the layouts.ejs file is the pair of comments: and in order to ensure that the dependencies loaded before my custom scripts (whose tags I placed in the bodies of their corresponding views,) I moved the script above the <%- body %> tag in layout.ejs so that the tag is inserted before the body of whatever view I am rendering. – aleph_one Oct 17 '14 at 19:23
  • Sounds like you got it – Meeker Oct 17 '14 at 23:07