Proper reactivity for calculated templates

This commit is contained in:
David Greenspan
2014-06-09 16:35:24 -07:00
parent f687e4198f
commit 4e8472e970
4 changed files with 41 additions and 45 deletions

View File

@@ -97,6 +97,11 @@ _.extend(CodeGen.prototype, {
} else {
var compCode = self.codeGenPath(path, {lookupTemplate: true});
if (path.length > 1) {
// capture reactivity
compCode = 'function () { return Spacebars.call(' + compCode +
'); }';
}
var dataCode = self.codeGenInclusionDataFunc(tag.args);
var content = (('content' in tag) ?
@@ -106,15 +111,13 @@ _.extend(CodeGen.prototype, {
var includeArgs = [compCode];
if (content) {
// XXX `null` will be the data arg when we change calling convention
includeArgs.push('null', content);
includeArgs.push(content);
if (elseContent)
includeArgs.push(elseContent);
}
var includeCode =
'Blaze.Isolate(function () { return Spacebars.include2(' +
includeArgs.join(', ') + '); })';
'Spacebars.include2(' + includeArgs.join(', ') + ')';
// calling convention compat -- set the data context around the
// entire inclusion, so that if the name of the inclusion is

View File

@@ -107,12 +107,9 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
run("{{#foo}}abc{{/foo}}",
function() {
var self = this;
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self),
null,
(function() { return "abc"; })
);
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self), (function() {
return "abc";
}));
});
run("{{#if cond}}aaa{{else}}bbb{{/if}}",
@@ -145,9 +142,7 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
return Spacebars.TemplateWith(function() {
return Spacebars.call(Blaze.lookup("bar", self));
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
});
@@ -159,11 +154,10 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
x: Spacebars.call(Blaze.lookup("bar", self))
};
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
});
}
);
run("{{> foo bar.baz}}",
function() {
@@ -171,9 +165,7 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
return Spacebars.TemplateWith(function() {
return Spacebars.call(Spacebars.dot(Blaze.lookup("bar", self), "baz"));
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
});
@@ -185,9 +177,7 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
x: Spacebars.call(Spacebars.dot(Blaze.lookup("bar", self), "baz"))
};
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
});
@@ -197,11 +187,10 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
return Spacebars.TemplateWith(function() {
return Spacebars.dataMustache(Blaze.lookup("bar", self), Blaze.lookup("baz", self));
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self));
});
});
}
);
run("{{#foo bar baz}}aaa{{/foo}}",
function() {
@@ -209,11 +198,9 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
return Spacebars.TemplateWith(function() {
return Spacebars.dataMustache(Blaze.lookup("bar", self), Blaze.lookup("baz", self));
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self), null, (function() {
return "aaa";
}));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self), (function() {
return "aaa";
}));
});
});
@@ -223,11 +210,9 @@ Tinytest.add("spacebars-compiler - compiler output", function (test) {
return Spacebars.TemplateWith(function() {
return Spacebars.dataMustache(Spacebars.dot(Blaze.lookup("p", self), "q"), Spacebars.dot(Blaze.lookup("r", self), "s"));
}, function() {
return Blaze.Isolate(function() {
return Spacebars.include2(Blaze.lookupTemplate("foo", self), null, (function() {
return "aaa";
}));
});
return Spacebars.include2(Blaze.lookupTemplate("foo", self), (function() {
return "aaa";
}));
});
});

View File

@@ -1149,7 +1149,7 @@ Tinytest.add('spacebars-tests - template_tests - inclusion helpers are isolated'
test.throws(function () {
Deps.flush({_throwFirstError: true});
}, /Expected null or template/);
}, /Expected template or null/);
});
Tinytest.add('spacebars-tests - template_tests - nully attributes', function (test) {

View File

@@ -1,12 +1,20 @@
Spacebars.include2 = function (templateOrFunction, dataFunc, contentFunc, elseFunc) {
var template = Spacebars.call(templateOrFunction);
if (template === null)
return null;
if (! (template instanceof Blaze.Component))
throw new Error("Expected template or null, found: " + template);
var tripleEquals = function (a, b) { return a === b; };
return new template.constructor(dataFunc, contentFunc, elseFunc);
Spacebars.include2 = function (templateOrFunction, contentFunc, elseFunc) {
var templateVar = Blaze.Var(templateOrFunction, tripleEquals);
return Blaze.Isolate(function () {
var template = templateVar.get();
if (template === null)
return null;
if (! (template instanceof Blaze.Component))
throw new Error("Expected template or null, found: " + template);
// Current calling convention (as of 0.8) means we have to wrap data
// around the whole inclusion, not pass it here (the `null` arg)
return new template.constructor(null, contentFunc, elseFunc);
});
};
// * `templateOrFunction` - template (component) or function returning a template