我正在使用 RequireJS 和 Mustache。模板的内容位于一些外部文件中,这些文件是通过文本插件加载的。
唯一让我有点恼火的是它所强加的目录结构。我的所有脚本都位于 js
目录中,如下所示
index.html
js
libs
require.js
text.js
jquery.js
...
controllers
...
views
...
...
因此我使用 baseUrl = 'js'
配置 RequireJS,以简化模块名称。但这迫使我在 js 目录中包含模板,否则它们对文本插件不可见。
有没有办法配置 RequireJS,以便在脚本目录之外的其他位置查找文本文件依赖项?
(当然我可以避免文本插件并手动定义AJAX请求来抓取文件,但这不是问题的重点。如果可能,我想使用现有的工具)
I am using RequireJS and Mustache in a Javascript application. The content of the templates is inside some external files, which are loaded via the text plugin.
The only thing that slightly annoys me is the directory structure this imposes. All my scripts live inside a js
directory, like this
index.html
js
libs
require.js
text.js
jquery.js
...
controllers
...
views
...
...
Hence I configure RequireJS with baseUrl = 'js'
, to simplify module names. But this force me to have templates inside the js
directory, otherwise they are not visible to the text plugin.
Is there a way to configure RequireJS so that text files dependencies are looked elsewhere than the scripts directory?
(Of course I could avoid the text plugin and manually define AJAX requests to grab the files, but this is not the point of the question. If possible, I would like to use the existing tools)
发布评论
评论(2)
您可以指定绝对路径。来自文档:
You can specify an absolute path. From the docs:
我通常将我的 baseUrl 设置为 。以 js 开头的脚本路径如下:
现在我可以将模板保存在 ./templates 中并轻松引用它们。
I usually set up my baseUrl to . and script paths starting with js like this:
Now I can keep templates in ./templates and reference them easily.