I am constructing a task app ( For fun) & i just sat down thinking about this problem. I will put the question on my mind in words here.
Model is extremely simple, it contains collection of Project
. Each Project contains a TaskList
these TaskList is nestable i.e for example a Task Design FrontPage can have Design Header as another TaskList
. Each TaskList contain Tasks
. How will a typical javascript template look like for this problem. I could not come with one that works this scenario. This problem is as same as N level nested Menu, how would you render it using templating library.
<div>
{{#Projects}}
<div>
<b>{{ProjectName}}</b>
</div>
<ul>
{{#TaskList}}
<li>{{TaskListName}} {{CreatedBy}} The Problem Comes here - How do i Render a #TaskList
here </li>
{{/TaskList}}
</ul>
{{/Projects}}
</div>
btw if anyone has asp.net solution (Ideas for this let me hear them), N level deep nesting is the thing i am unable to overcome right now.
You could define your TaskList
as a partial and include it recursively as the documentation hints.
Templates:
<script type="text/template" id="projects">
{{#Projects}}
<div>
Project: <b>{{ProjectName}}</b>
</div>
{{>taskList}}
{{/Projects}}
</script>
<script type="text/template" id="task-list">
{{#TaskList}}
<ul>
<li>
{{TaskListName}} <em>{{CreatedBy}}</em>
{{>taskList}}
</li>
</ul>
{{/TaskList}}
</script>
JavaScript:
var data = {
Projects: [
{
ProjectName: "Project 1",
TaskList: [{
TaskListName: "Name 1",
CreatedBy: "Person 1"},
{
TaskListName: "Name 2",
CreatedBy: "Person 2",
TaskList: [{
TaskListName: "Sub Name",
CreatedBy: "Same Person"},
{
TaskListName: "Sub Name 2",
CreatedBy: "Person 1"},
{
TaskListName: "Sub Name 3",
CreatedBy: "Person 3-2",
TaskList: [{
TaskListName: "Sub Sub Name",
CreatedBy: "Person 3-3"}]}]}]},
{
ProjectName: "Project 2",
TaskList: [{
TaskListName: "Name 3",
CreatedBy: "Person 3"},
{
TaskListName: "Name 4",
CreatedBy: "Person 4"}]}]
},
template = $('#projects').html(),
partials = {
taskList: $('#task-list').html()
},
html = Mustache.render(template, data, partials);
document.write(html);
Here's the jsFiddle to see it in action- http://jsfiddle.net/maxbeatty/ND7xv/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With