Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS Text Plugin installed with Bower

How should I be using requirejs-text which is installed via bower? I am supposed to put it in baseUrl but wonder if I could use it from components/requirejs-text/? Whats the best practice?

like image 923
Jiew Meng Avatar asked May 13 '13 02:05

Jiew Meng


3 Answers

Define the path to the plugin in the config:

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

And use it in your module as documented on 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.
    }
);

You can use also technically use the plugin without the path definition in the requirejs.config, but this is propbably not best practice:

require(["your_path_to_the_plugin_from_baseurl/without_js_at_the_end!some/textfile"],
    function(yourTextfile) {
    }
);
like image 104
Uli Avatar answered Nov 15 '22 22:11

Uli


in PROJECT_APP/bower.js add this line under the dependencies section:

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

then run bower install, it should install this plugin and display at the end a path such as requirejs-text#2.0.10 vendor/bower/requirejs-text (depends on your configuration).

Finally, in the config.js file, add this line under

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"
    }

});

Then to use it, simply require it, in this case you can access it with the template variable

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 !
like image 22
gasp Avatar answered Nov 15 '22 21:11

gasp


This is how I have install requirejs-text using bower

In your project's bower.json file:

{
    "name":"{{YOUR PROJECT NAME}}",
    "version":"{{YOUR PROJECT VERSION}}",
    "dependencies":{
        "requirejs-text":"2.0.6"
     }
}
like image 38
Ian Lim Avatar answered Nov 15 '22 22:11

Ian Lim