Skip to content
Browse files

Merge pull request #24 from concordusapps/master

Directly returning AMD templates from the wrapper
  • Loading branch information...
2 parents 36a12c0 + 11bc328 commit 677b6ec8fc98b6aaa86549f7f2f3125e61937fec @tkellen tkellen committed Feb 8, 2013
Showing with 62 additions and 22 deletions.
  1. +29 −20 Gruntfile.js
  2. +3 −1 docs/handlebars-options.md
  3. +10 −1 tasks/handlebars.js
  4. +10 −0 test/expected/amd_compile_direct.js
  5. +10 −0 test/handlebars_test.js
View
49 Gruntfile.js
@@ -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
4 docs/handlebars-options.md
@@ -12,6 +12,8 @@ Default: 'JST'
The namespace in which the precompiled templates will be assigned. *Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping.*
+No namespace wrapping together with an AMD wrapper will result in the template being returned directly from the wrapper.
+
Example:
```js
options: {
@@ -37,7 +39,7 @@ For this option to work you need to define the `namespace` option.
Type: `Boolean`
default: `false`
-Wraps the output file with an AMD define function and returns the compiled template.
+Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.
```js
define(function() {
View
11 tasks/handlebars.js
@@ -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("});");
}
View
10 test/expected/amd_compile_direct.js
@@ -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
10 test/handlebars_test.js
@@ -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 677b6ec

Please sign in to comment.
Something went wrong with that request. Please try again.