PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/app/examples/public/friendlycode/js/require-plugins/template.js

https://github.com/iwatchallit/towtruck
JavaScript | 31 lines | 27 code | 3 blank | 1 comment | 3 complexity | 4da2e9cb36d96541be23c0bc6ed17e5f MD5 | raw file
  1. // This is a simple RequireJS plugin that loads an underscore.js template.
  2. define(["module", "text", "underscore"], function (module, text, _) {
  3. var buildMap = {},
  4. masterConfig = module.config();
  5. return {
  6. load: function(name, req, onLoad, config) {
  7. var url = req.toUrl("templates/" + name).replace(".js", ".html");
  8. text.get(url, function (data) {
  9. var template;
  10. if (config.isBuild) {
  11. template = buildMap[name] = "_.template(" + JSON.stringify(data) +
  12. ")";
  13. } else {
  14. template = _.template(data);
  15. }
  16. onLoad(template);
  17. });
  18. },
  19. write: function (pluginName, moduleName, write) {
  20. if (buildMap[moduleName]) {
  21. var content = buildMap[moduleName];
  22. write.asModule(pluginName + "!" + moduleName,
  23. "define(['underscore'], function (_) { \n return " + content +
  24. ";});\n");
  25. }
  26. }
  27. };
  28. });