Как мне использовать requirejs-text
, который устанавливается через Bower? Я должен поместить его в baseUrl
, но интересно, смогу ли я использовать его из components/requirejs-text/
? Какая лучшая практика?
Плагин RequireJS Text, установленный с Bower
Ответы (3)
Указываем путь к плагину в конфиге:
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) {
}
);
в 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 !
"vendor": "../vendor"
помогает упростить доступ к поставщику? В любом случае вам не потребуется каталог поставщика.
- person bartzy; 30.03.2014
Вот как я установил requirejs-text с помощью Bower
В файле вашего проекта bower.json:
{
"name":"{{YOUR PROJECT NAME}}",
"version":"{{YOUR PROJECT VERSION}}",
"dependencies":{
"requirejs-text":"2.0.6"
}
}
baseUrl
приложения, но, поскольку он находится в беседке components
, как мне его использовать?
- person Jiew Meng; 11.06.2013
requirejs-text
с версией 2.0.10.
- person kaiser; 08.08.2013