Permalink
Browse files

AMD templates are now returned directly when namespace is set to false.

  • Loading branch information...
1 parent 0b4af70 commit ecc81efd52d5b388cd0caf81884f3ff8a03e7af2 @mehcode mehcode committed Feb 3, 2013
Showing with 59 additions and 21 deletions.
  1. +29 −20 Gruntfile.js
  2. +10 −1 tasks/handlebars.js
  3. +10 −0 test/expected/amd_compile_direct.js
  4. +10 −0 test/handlebars_test.js
View
@@ -84,26 +84,35 @@ module.exports = function(grunt) {
files: {
'tmp/no_namespace.js': ['test/fixtures/basic.hbs']
}
- },
- processcontent: {
- options: {
- processContent: function(content) {
- content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
- content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]+$/, '');
- return content;
- }
- },
- files: {
- 'tmp/processcontent.js': ['test/fixtures/has-spaces.hbs']
- }
- },
- amd_compile: {
- options: {
- amd: true
- },
- files: {
- 'tmp/amd_compile.js': ['test/fixtures/amd.html']
- }
+ },
+ processcontent: {
+ options: {
+ processContent: function(content) {
+ content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
+ content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]+$/, '');
+ return content;
+ }
+ },
+ files: {
+ 'tmp/processcontent.js': ['test/fixtures/has-spaces.hbs']
+ }
+ },
+ amd_compile: {
+ options: {
+ amd: true
+ },
+ files: {
+ 'tmp/amd_compile.js': ['test/fixtures/amd.html']
+ }
+ },
+ amd_compile_direct: {
+ options: {
+ amd: true,
+ namespace: false
+ },
+ files: {
+ 'tmp/amd_compile_direct.js': ['test/fixtures/amd.html']
+ }
}
},
// Unit tests.
View
@@ -70,6 +70,11 @@ module.exports = function(grunt) {
if (options.wrapped) {
compiled = 'Handlebars.template('+compiled+')';
}
+ // if configured for amd and the namespace has been explicitly set
+ // to false, the handlebars template will be directly returned
+ if (options.wrapped && options.amd && options.namespace === false) {
+ compiled = 'return ' + compiled;
+ }
} catch (e) {
grunt.log.error(e);
grunt.fail.warn('Handlebars failed to compile '+filepath+'.');
@@ -110,7 +115,11 @@ module.exports = function(grunt) {
if (options.amd) {
// Wrap the file in an AMD define fn.
output.unshift("define(['handlebars'], function(Handlebars) {");
- output.push("return "+nsInfo.namespace+";");
+ if (options.namespace !== false) {
+ // Namespace has not been explicitly set to false; the AMD
+ // wrapper will return the object containing the template.
+ output.push("return "+nsInfo.namespace+";");
+ }
output.push("});");
}
@@ -0,0 +1,10 @@
+define(['handlebars'], function(Handlebars) {
+
+return Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ helpers = helpers || Handlebars.helpers; data = data || {};
+
+
+
+ return "<section class=\"main-app\">\n <h1>Some title</h1>\n <p>I've been compiled with amd support</p>\n</section>";})
+
+});
View
@@ -90,5 +90,15 @@ exports.handlebars = {
test.equal(actual, expected, 'should wrap everything with an AMD define block.');
test.done();
+ },
+ amd_compile_direct: function(test) {
+ 'use strict';
+ test.expect(1);
+
+ var actual = grunt.file.read('tmp/amd_compile_direct.js');
+ var expected = grunt.file.read('test/expected/amd_compile_direct.js'); // same as previous test
+ test.equal(actual, expected, 'should wrap everything with an AMD define block and directly return the template.');
+
+ test.done();
}
};

0 comments on commit ecc81ef

Please sign in to comment.