如何在SailsJS中选择性地添加JavaScript资源? [英] How can I include javascript assets selectively in SailsJS?

查看:430
本文介绍了如何在SailsJS中选择性地添加JavaScript资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Sails.js应用程序中,如何有选择地包含JavaScript资源?



例如,如果我有一个管理页面,并且admin.js位于assets / js目录中。我如何保持admin.js加载到公共索引页面?



我知道我可以将js移到公共目录,并且包括脚本在我的管理视图的模板。但是,在 assets.js()调用插入JavaScript之后,我仍然无法添加它。我需要在 sails.io.js 脚本加载后插入。



有没有办法选择性地加载脚本,但仍然有访问 assets.js()函数调用中自动包含的 sails.io.js 这种情况有更好的范式吗?



编辑:



自从发布SailsJS 0.9和资产管理体系的重组,这个问题并不真正适用。

解决方案

我知道这是一个老问题,但由于人们仍然在寻找。



在创建新项目后,Sails在根目录中有一个名为tasks的文件夹。



您要查找的文件是


pipeline.js


该文件包含一个名为的变量jsFilesToInject

  //按照顺序
//使用客户端JavaScript文件(使用Grunt风格的通配符/ glob / splat表达式)
var jsFilesToInject = [

//在所有其他东西之前加载sails.io
//'js / dependencies / sails.io.js',
'js / sails.io.js'


//像jQuery或Angular这样的依赖关系被带入
'js / dependencies / ** / *。js',

//您的客户端js文件
//的所有其余部分将不会以特定顺序注入。
'js / ** / *。js'
];

只需将您要在sails.io.js之前加载的脚本放在一起。



这与sails相关0.11.x



另一种有效的方式是创建一个views / partials文件夹

并创建一个新的文件,如mapScripts.ejs
将脚本标签放在那里,并在您的视图中使用

 <%include ../partials/mapScripts%> 


In a Sails.js application, how can I include javascript assets selectively?

For instance, if I have an admin page and admin.js lives inside the assets/js directory. How do I keep the admin.js from loading on the public index page?

I'm aware that I could move the js out to the public directory, and include the script in my admin view's template. But I'm still unable to include it after the assets.js() call inserts it's javascript. I need it to be inserted after the sails.io.js script is loaded.

Is there any way to selectively load scripts and still have access to the sails.io.js which is automatically included with the assets.js() function call? Is there a better paradigm for this kind of situation?

EDIT:

Since the release of SailsJS 0.9 and the restructuring of the asset management system, this question doesn't really apply anymore.

解决方案

I know this is a old question but since people are still looking for this.

Sails has a folder called tasks in the root after you create a new project.

The file you are looking for is

pipeline.js

That file holds a variable called jsFilesToInject

// Client-side javascript files to inject in order
// (uses Grunt-style wildcard/glob/splat expressions)
var jsFilesToInject = [

  // Load sails.io before everything else
  // 'js/dependencies/sails.io.js',
  'js/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'
];

Just put your script that you want loaded before sails.io.js.

This is relevant for sails 0.11.x

another way that is valid is to create a views/partials folder

and create a new file like mapScripts.ejs drop the script tags there and in your view use

<% include ../partials/mapScripts %>

这篇关于如何在SailsJS中选择性地添加JavaScript资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆