문제

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)

도움이 되었습니까?

해결책

You can specify an absolute path. From the docs:

However, if the dependency name has one of the following properties, it is treated as a regular file path, like something that was passed to a tag:

  • Ends in ".js"
  • Starts with a "/"
  • Contains an URL protocol, like "http:" or "https:"

다른 팁

I usually set up my baseUrl to . and script paths starting with js like this:

require.config({
    baseUrl: ".",
    paths: {
        "lodash": "js/ext/lodash/dist/lodash",
        "jquery": "js/ext/jquery/jquery",
        "domReady": "js/ext/requirejs-domready/domReady",
        "text": "js/ext/requirejs-text/text.js"
    }
});

Now I can keep templates in ./templates and reference them easily.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top