2013-05-13 15 views

risposta

25

Definire il percorso del plug-in di configurazione:

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

E usarlo nel modulo come documentato su 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. 
    } 
); 

È possibile utilizzare anche tecnicamente usare il plugin, senza la definizione percorso nel requirejs.config, ma questo è propbably non migliori pratiche:

require(["your_path_to_the_plugin_from_baseurl/without_js_at_the_end!some/textfile"], 
    function(yourTextfile) { 
    } 
); 
2

Questo è come devo installare requirejs-testo utilizzando pergolato

Nel file di bower.json del progetto:

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

Volevo chiedere come configurare requireJS per utilizzare il plug-in di testo. Capisco che sia stato messo in app 'baseUrl', ma dal momento che è in bower' components', come lo uso? –

+0

bower ti aiuta solo a recuperare tutti i file (sì l'intero Github) al tuo ambiente locale. Potrebbe essere ancora necessario configurare un altro strumento per estrarlo ulteriormente. Un esempio di tale strumento è [grunt-bower-task] (https://github.com/yatskevich/grunt-bower-task). Successivamente, configura il plug-in di testo in requirejs.config come al solito. –

+0

Oltre al plug-in di testo, si può anche prendere in considerazione il plugin requirejs-tpl https://github.com/jfparadis/requirejs-tpl che è più conveniente usare –

3

in PROJECT_APP/bower.js aggiungere questa riga nella sezione dipendenze:

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

quindi eseguire bower install, dovrebbe installare questo plugin e visualizzare alla fine un percorso come requirejs-text#2.0.10 vendor/bower/requirejs-text (dipende dalla configurazione).

Infine, nel file config.js, aggiungere questa riga sotto

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

}); 

Quindi per utilizzarlo, è sufficiente richiederlo, in questo caso, è possibile accedere con il template variabile

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 ! 
+1

Come "fornitore": "../ fornitore" 'aiuta a rendere il fornitore" più facile da accedere "? In ogni caso non è richiesta la directory del fornitore. – bartzy

Problemi correlati