Подтвердить что ты не робот

Текстовый плагин RequireJS, установленный с Bower

Как я должен использовать requirejs-text, который устанавливается через bower? Я должен положить его в baseUrl, но задаюсь вопросом, могу ли я использовать его из components/requirejs-text/? Какая самая лучшая практика?

4b9b3361

Ответ 1

Определите путь к плагину в 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) {
    }
);

Ответ 2

в 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 !

Ответ 3

Вот как я устанавливаю requirejs-text с помощью bower

В вашем проекте файл bower.json:

{
    "name":"{{YOUR PROJECT NAME}}",
    "version":"{{YOUR PROJECT VERSION}}",
    "dependencies":{
        "requirejs-text":"2.0.6"
     }
}