Permalink
Browse files

Move tag helper generator out of loop so that it's defined only once.

  • Loading branch information...
1 parent f1749e2 commit 62e7d142fd030b74096130abee86935b9fe0bd3f @savetheclocktower savetheclocktower committed with erikvold Jan 22, 2010
Showing with 33 additions and 34 deletions.
  1. +33 −34 src/Template.js
View
@@ -86,46 +86,45 @@ Jaml.Template.prototype = {
*/
(function() {
var tags = Jaml.Template.prototype.tags;
-
- for (var i = tags.length - 1; i >= 0; i--){
- var tagName = tags[i];
-
- /**
- * This function is created for each tag name and assigned to Template's
- * prototype below
- */
- var fn = function(tagName) {
- return function(attrs) {
- var node = new Jaml.Node(tagName);
-
- var firstArgIsAttributes = (typeof attrs == 'object')
- && !(attrs instanceof Jaml.Node)
- && !(attrs instanceof Jaml.TextNode);
- if (firstArgIsAttributes) node.setAttributes(attrs);
+ /**
+ * This function is created for each tag name and assigned to Template's
+ * prototype below
+ */
+ var makeTagHelper = function(tagName) {
+ return function(attrs) {
+ var node = new Jaml.Node(tagName);
+
+ var firstArgIsAttributes = (typeof attrs == 'object')
+ && !(attrs instanceof Jaml.Node)
+ && !(attrs instanceof Jaml.TextNode);
+
+ if (firstArgIsAttributes) node.setAttributes(attrs);
- var startIndex = firstArgIsAttributes ? 1 : 0;
+ var startIndex = firstArgIsAttributes ? 1 : 0;
- for (var i=startIndex; i < arguments.length; i++) {
- var arg = arguments[i];
+ for (var i=startIndex; i < arguments.length; i++) {
+ var arg = arguments[i];
- if (typeof arg == "string" || arg == undefined) {
- arg = new Jaml.TextNode(arg || "");
- }
-
- if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) {
- arg.parent = node;
- }
+ if (typeof arg == "string" || arg == undefined) {
+ arg = new Jaml.TextNode(arg || "");
+ }
- node.addChild(arg);
- };
-
- this.nodes.push(node);
-
- return node;
+ if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) {
+ arg.parent = node;
+ }
+
+ node.addChild(arg);
};
+
+ this.nodes.push(node);
+
+ return node;
};
-
- Jaml.Template.prototype[tagName] = fn(tagName);
+ };
+
+ for (var i = tags.length - 1; i >= 0; i--){
+ var tagName = tags[i];
+ Jaml.Template.prototype[tagName] = makeTagHelper(tagName);
};
})();

0 comments on commit 62e7d14

Please sign in to comment.