Skip to content

Commit

Permalink
Move tag helper generator out of loop so that it's defined only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
savetheclocktower authored and erikvold committed Jan 29, 2011
1 parent f1749e2 commit 62e7d14
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions src/Template.js
Expand Up @@ -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.