-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
{{> UI.dynamic }} for easier dynamic template picking.
See #2123.
- Loading branch information
Emily Stark
committed
May 28, 2014
1 parent
b63328c
commit bd4a873
Showing
7 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.build* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template name="ui-dynamic-test"> | ||
{{> UI.dynamic templateName=templateName dataContext=templateData}} | ||
</template> | ||
|
||
<template name="ui-dynamic-test-no-data"> | ||
{{> UI.dynamic templateName=templateName}} | ||
</template> | ||
|
||
<template name="ui-dynamic-test-inherited-data"> | ||
{{#with context}} | ||
{{> UI.dynamic templateName=templateName}} | ||
{{else}} | ||
{{> UI.dynamic templateName=templateName}} | ||
{{/with}} | ||
</template> | ||
|
||
<template name="ui-dynamic-test-sub"> | ||
test{{foo}} | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copied from spacebars-tests | ||
var renderToDiv = function (comp) { | ||
var div = document.createElement("DIV"); | ||
UI.materialize(comp, div); | ||
return div; | ||
}; | ||
|
||
Tinytest.add( | ||
"ui-dynamic-template - render template dynamically", function (test, expect) { | ||
var tmpl = Template["ui-dynamic-test"]; | ||
|
||
var rvName = new ReactiveVar; | ||
var rvData = new ReactiveVar; | ||
tmpl.templateName = function () { | ||
return rvName.get(); | ||
}; | ||
tmpl.templateData = function () { | ||
return rvData.get(); | ||
}; | ||
|
||
// No template chosen | ||
var div = renderToDiv(tmpl); | ||
test.equal(div.innerHTML.trim(), ""); | ||
|
||
// Choose the "ui-dynamic-test-sub" template, with no data context | ||
// passed in. | ||
rvName.set("ui-dynamic-test-sub"); | ||
Deps.flush(); | ||
test.equal(div.innerHTML.trim(), "test"); | ||
|
||
// Set a data context. | ||
rvData.set({ foo: "bar" }); | ||
Deps.flush(); | ||
test.equal(div.innerHTML.trim(), "testbar"); | ||
}); | ||
|
||
// Same test as above, but the {{> UI.dynamic}} inclusion has no | ||
// `dataContext` argument. | ||
Tinytest.add( | ||
"ui-dynamic-template - render template dynamically, no data context", | ||
function (test, expect) { | ||
var tmpl = Template["ui-dynamic-test-no-data"]; | ||
|
||
var rvName = new ReactiveVar; | ||
tmpl.templateName = function () { | ||
return rvName.get(); | ||
}; | ||
|
||
var div = renderToDiv(tmpl); | ||
test.equal(div.innerHTML.trim(), ""); | ||
|
||
rvName.set("ui-dynamic-test-sub"); | ||
Deps.flush(); | ||
test.equal(div.innerHTML.trim(), "test"); | ||
}); | ||
|
||
|
||
Tinytest.add( | ||
"ui-dynamic-template - render template " + | ||
"dynamically, data context gets inherited", | ||
function (test, expect) { | ||
var tmpl = Template["ui-dynamic-test-inherited-data"]; | ||
|
||
var rvName = new ReactiveVar(); | ||
var rvData = new ReactiveVar(); | ||
tmpl.templateName = function () { | ||
return rvName.get(); | ||
}; | ||
tmpl.context = function () { | ||
return rvData.get(); | ||
}; | ||
|
||
var div = renderToDiv(tmpl); | ||
test.equal(div.innerHTML.trim(), ""); | ||
|
||
rvName.set("ui-dynamic-test-sub"); | ||
Deps.flush(); | ||
test.equal(div.innerHTML.trim(), "test"); | ||
|
||
// Set the top-level template's data context; this should be | ||
// inherited by the dynamically-chosen template, since the {{> | ||
// UI.dynamic}} inclusion didn't include a data argument. | ||
rvData.set({ foo: "bar" }); | ||
Deps.flush(); | ||
test.equal(div.innerHTML.trim(), "testbar"); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<template name="__dynamicWithDataContext"> | ||
{{#with chooseTemplate templateName}} | ||
{{#with ../dataContext}} {{! original 'dataContext' argument to __dynamic}} | ||
{{> ..}} {{! return value from chooseTemplate(templateName) }} | ||
{{else}} {{! if the 'dataContext' argument was falsey }} | ||
{{> .}} {{! return value from chooseTemplate(templateName) }} | ||
{{/with}} | ||
{{/with}} | ||
</template> | ||
|
||
<template name="__dynamic"> | ||
{{#if dataContext}} | ||
{{> __dynamicWithDataContext}} | ||
{{else}} | ||
{{! if there was no explicit dataContext argument, use the parent context}} | ||
{{> __dynamicWithDataContext templateName=templateName dataContext=..}} | ||
{{/if}} | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Template.__dynamicWithDataContext.chooseTemplate = function (name) { | ||
return Template[name] || null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Package.describe({ | ||
summary: "Component for dynamically rendering templates", | ||
internal: true | ||
}); | ||
|
||
Package.on_use(function (api) { | ||
api.use('templating'); | ||
api.add_files(['dynamic.html', 'dynamic.js'], 'client'); | ||
}); | ||
|
||
Package.on_test(function (api) { | ||
api.use(["ui-dynamic-template", "tinytest", "test-helpers"]); | ||
api.use("templating", "client"); | ||
api.add_files(["dynamic-tests.html", "dynamic-tests.js"], "client"); | ||
}); |