11

requirejs-textbower経由でインストールされたものをどのように使用すればよいですか? 入れるはずなのbaseUrlですが から使えるのかなcomponents/requirejs-text/?ベストプラクティスは何ですか?

4

3 に答える 3

25

config でプラグインへのパスを定義します。

requirejs.config({
    paths: {
        "text" : "components/requirejs-text/text"
    }
},

https://github.com/requirejs/textに記載されているように、モジュールで使用します。

require(["some/module", "text!some/module.html", "text!some/module.css"],
    function(module, html, css) {
        //the html variable will be the text
        //of the some/module.html file
        //the css variable will be the text
        //of the some/module.css file.
    }
);

技術的には、requirejs.config でパスを定義せずにプラグインを使用することもできますが、これはおそらくベスト プラクティスではありません。

require(["your_path_to_the_plugin_from_baseurl/without_js_at_the_end!some/textfile"],
    function(yourTextfile) {
    }
);
于 2013-06-10T15:27:52.690 に答える
3

依存関係セクションの下に次の行をPROJECT_APP/bower.js追加します。

"requirejs": "~2.1.8",
"requirejs-text":"~2.0.10", // this is new
"qunit": "~1.12.0",

を実行するbower installと、このプラグインがインストールされ、最後に次のようなパスが表示さrequirejs-text#2.0.10 vendor/bower/requirejs-textれます (構成によって異なります)。

最後に、config.js ファイルで、この行を以下に追加します。

require.config({
    paths: {

        // Make vendor easier to access.
        "vendor": "../vendor",
        // Almond is used to lighten the output filesize.
        "almond": "../vendor/bower/almond/almond",

         // add the requirejs text plugin here 
         "text" : "../vendor/bower/requirejs-text/text",

        // Opt for Lo-Dash Underscore compatibility build over Underscore.
        "underscore": "../vendor/bower/lodash/dist/lodash.underscore",

        // Map remaining vendor dependencies.
        "jquery": "../vendor/bower/jquery/jquery",
        "backbone": "../vendor/bower/backbone/backbone"
    }

});

次に、それを使用するには、単にそれを要求します。この場合、template変数でアクセスできます

define([
    // These are path alias that we configured in our bootstrap
    'app',        // general app variables
    'jquery',     // lib/jquery/jquery
    'underscore', // lib/underscore/underscore
    'backbone',   // lib/backbone/backbone
    'text!templates/books.html' // use the plugin to import a template
], function(app,$, _, Backbone, template){ // don't forget to define it !
于 2013-10-07T16:28:31.353 に答える
2

これは、bowerを使用してrequirejs-textをインストールする方法です

プロジェクトの bower.json ファイルで:

{
    "name":"{{YOUR PROJECT NAME}}",
    "version":"{{YOUR PROJECT VERSION}}",
    "dependencies":{
        "requirejs-text":"2.0.6"
     }
}
于 2013-06-06T09:14:18.040 に答える