Update specs for change to template signature

This commit is contained in:
tomhuda
2011-05-03 20:39:14 -07:00
parent 5d99572006
commit c29223a98b
2 changed files with 43 additions and 22 deletions
+15
View File
@@ -183,6 +183,21 @@ annotations for webkit browsers, but will slightly increase startup
time. time.
Upgrading
---------
When upgrading from the Handlebars 0.9 series, be aware that the
signature for passing custom helpers or partials to templates has
changed.
Instead of:
template(context, helpers, partials, [data])
Use:
template(context, {helpers: helpers, partials: partials, data: data}
Known Issues Known Issues
------------ ------------
* Handlebars.js can be cryptic when there's an error while rendering. * Handlebars.js can be cryptic when there's an error while rendering.
+28 -22
View File
@@ -6,19 +6,25 @@ Handlebars.registerHelper('helperMissing', function(helper, context) {
} }
}); });
var shouldCompileTo = function(string, hash, expected, message) { var shouldCompileTo = function(string, hashOrArray, expected, message) {
var template = Handlebars.compile(string); var template = Handlebars.compile(string), ary;
if(Object.prototype.toString.call(hash) === "[object Array]") { if(Object.prototype.toString.call(hashOrArray) === "[object Array]") {
if(hash[1]) { helpers = hashOrArray[1];
if(helpers) {
for(var prop in Handlebars.helpers) { for(var prop in Handlebars.helpers) {
hash[1][prop] = Handlebars.helpers[prop]; helpers[prop] = Handlebars.helpers[prop];
} }
} }
ary = [];
ary.push(hashOrArray[0]);
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
} else { } else {
hash = [hash]; ary = [hashOrArray];
} }
result = template.apply(this, hash) result = template.apply(this, ary);
equal(result, expected, "'" + expected + "' should === '" + result + "': " + message); equal(result, expected, "'" + expected + "' should === '" + result + "': " + message);
}; };
@@ -532,7 +538,7 @@ test("passing in data to a compiled function that expects data - works with help
} }
}; };
var result = template({noun: "cat"}, helpers, null, {adjective: "happy"}); var result = template({noun: "cat"}, {helpers: helpers, data: {adjective: "happy"}});
equals("happy cat", result); equals("happy cat", result);
}); });
@@ -545,7 +551,7 @@ test("passing in data to a compiled function that expects data - works with help
} }
}; };
var result = template({exclaim: true, world: "world"}, helpers, null, {adjective: "happy"}); var result = template({exclaim: true, world: "world"}, {helpers: helpers, data: {adjective: "happy"}});
equals("happy world!", result); equals("happy world!", result);
}); });
@@ -561,7 +567,7 @@ test("passing in data to a compiled function that expects data - works with bloc
} }
}; };
var result = template({exclaim: true}, helpers, null, {adjective: "happy"}); var result = template({exclaim: true}, {helpers: helpers, data: {adjective: "happy"}});
equals("happy world!", result); equals("happy world!", result);
}); });
@@ -577,7 +583,7 @@ test("passing in data to a compiled function that expects data - works with bloc
} }
}; };
var result = template({exclaim: true, zomg: "world"}, helpers, null, {adjective: "happy"}); var result = template({exclaim: true, zomg: "world"}, {helpers: helpers, data: {adjective: "happy"}});
equals("happy world?", result); equals("happy world?", result);
}); });
@@ -593,7 +599,7 @@ test("passing in data to a compiled function that expects data - data is passed
} }
}; };
var result = template({exclaim: true, zomg: "world"}, helpers, null, {adjective: "happy", accessData: "#win"}); var result = template({exclaim: true, zomg: "world"}, {helpers: helpers, data: {adjective: "happy", accessData: "#win"}});
equals("#win happy world?", result); equals("#win happy world?", result);
}); });
@@ -609,7 +615,7 @@ test("you can override inherited data when invoking a helper", function() {
} }
}; };
var result = template({exclaim: true, zomg: "planet"}, helpers, null, {adjective: "happy"}); var result = template({exclaim: true, zomg: "planet"}, {helpers: helpers, data: {adjective: "happy"}});
equals("sad world?", result); equals("sad world?", result);
}); });
@@ -626,7 +632,7 @@ test("you can override inherited data when invoking a helper with depth", functi
} }
}; };
var result = template({exclaim: true, zomg: "world"}, helpers, null, {adjective: "happy"}); var result = template({exclaim: true, zomg: "world"}, {helpers: helpers, data: {adjective: "happy"}});
equals("sad world?", result); equals("sad world?", result);
}); });
@@ -648,7 +654,7 @@ test("helpers take precedence over same-named context properties", function() {
world: "world" world: "world"
}; };
var result = template(context, helpers); var result = template(context, {helpers: helpers});
equals(result, "GOODBYE cruel WORLD"); equals(result, "GOODBYE cruel WORLD");
}); });
@@ -670,7 +676,7 @@ test("helpers take precedence over same-named context properties", function() {
world: "world" world: "world"
}; };
var result = template(context, helpers); var result = template(context, {helpers: helpers});
equals(result, "GOODBYE cruel WORLD"); equals(result, "GOODBYE cruel WORLD");
}); });
@@ -685,7 +691,7 @@ test("helpers can take an optional hash", function() {
var context = {}; var context = {};
var result = template(context, helpers); var result = template(context, {helpers: helpers});
equals(result, "GOODBYE CRUEL WORLD"); equals(result, "GOODBYE CRUEL WORLD");
}); });
@@ -698,7 +704,7 @@ test("block helpers can take an optional hash", function() {
} }
}; };
var result = template({}, helpers); var result = template({}, {helpers: helpers});
equals(result, "GOODBYE CRUEL world"); equals(result, "GOODBYE CRUEL world");
}); });
@@ -711,7 +717,7 @@ test("arguments to helpers can be retrieved from options hash in string form", f
} }
}; };
var result = template({}, helpers); var result = template({}, {helpers: helpers});
equals(result, "HELP ME MY BOSS is.a slave.driver"); equals(result, "HELP ME MY BOSS is.a slave.driver");
}); });
@@ -726,7 +732,7 @@ test("when using block form, arguments to helpers can be retrieved from options
} }
}; };
var result = template({}, helpers); var result = template({}, {helpers: helpers});
equals(result, "HELP ME MY BOSS is.a slave.driver: help :("); equals(result, "HELP ME MY BOSS is.a slave.driver: help :(");
}); });
@@ -749,7 +755,7 @@ test("when inside a block in String mode, .. passes the appropriate context in t
dale: {}, dale: {},
need: 'need-a' need: 'need-a'
}, helpers); }, {helpers: helpers});
equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke"); equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke");
}); });
@@ -773,7 +779,7 @@ test("when inside a block in String mode, .. passes the appropriate context in t
dale: {}, dale: {},
need: 'need-a' need: 'need-a'
}, helpers); }, {helpers: helpers});
equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke wot"); equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke wot");
}); });