component inclusion code gen

This commit is contained in:
David Greenspan
2013-06-17 13:10:35 -07:00
parent cbfd6e5f61
commit efa83a5167
2 changed files with 46 additions and 13 deletions

View File

@@ -573,13 +573,13 @@ Spacebars.compile = function (inputString) {
return code;
};
var codeGenBasicStache = function (tag, funcInfo) {
var code = codeGenPath(tag.path, funcInfo);
// returns: array of source strings, or null if no
// args at all
var codeGenArgs = function (tagArgs, funcInfo, forComponent) {
var options = null; // source -> source
var args = null; // [source]
_.each(tag.args, function (arg) {
_.each(tagArgs, function (arg, i) {
var argType = arg[0];
var argValue = arg[1];
@@ -605,19 +605,40 @@ Spacebars.compile = function (inputString) {
options[toJSLiteral(arg[2])] = argCode;
} else {
// positional argument
args = (args || []);
args.push(argCode);
if (forComponent) {
// for Components, only take one positional
// argument, and call it `data`
if (i === 0) {
options = (options || {});
options[toJSLiteral('data')] = argCode;
}
} else {
args = (args || []);
args.push(argCode);
}
}
});
if (options) {
args = (args || []);
args.push(makeObjectLiteral(options));
if (forComponent) {
// components get one argument, the options dictionary
args = [options ? makeObjectLiteral(options) : '{}'];
} else {
// put options as dictionary at end of args
if (options) {
args = (args || []);
args.push(makeObjectLiteral(options));
}
}
code = 'String(Spacebars.call(' + code +
(args ? ', ' + args.join(', ') : '') + '))';
return code;
return args;
};
var codeGenBasicStache = function (tag, funcInfo) {
var nameCode = codeGenPath(tag.path, funcInfo);
var argCode = codeGenArgs(tag.args, funcInfo);
return 'String(Spacebars.call(' + nameCode +
(argCode ? ', ' + argCode.join(', ') : '') + '))';
};
// Return the source code of a string or (reactive) function
@@ -692,7 +713,11 @@ Spacebars.compile = function (inputString) {
} else {
switch (tag.type) {
case 'INCLUSION':
// XXX implement
var nameCode = codeGenPath(tag.path, funcInfo);
var argCode =
codeGenArgs(tag.args, funcInfo, true);
bodyLines.push('buf.component(function () { return ((' + nameCode + ') || EmptyComponent).create(' +
(argCode ? argCode.join(', ') : '') + '); });');
break;
case 'DOUBLE':
case 'TRIPLE':

View File

@@ -400,4 +400,12 @@ Tinytest.add("spacebars - compiler", function (test) {
' var self = this;',
' buf.text(function () { return String(Spacebars.call(Spacebars.index(self.lookup("foo"), "bar"), Spacebars.call(Spacebars.index(self.lookup("x"), "y")), 0, null, "hi", {"abc": Spacebars.call(Spacebars.index(self.lookup("z"), "w")), "z": 123.4})); });',
'}');
run('{{> foo bar baz=x.y}}',
'function (buf) {',
' var self = this;',
' buf.component(function () { return ((self.lookup("foo")) || EmptyComponent).create({"data": Spacebars.call(self.lookup("bar")), "baz": Spacebars.call(Spacebars.index(self.lookup("x"), "y"))}); });',
'}');
});