sorting nested {{#each}} in Meteor? -
in meteors guide found code below , wondering if todo.tags sorted somehow, maybe helpers method?
{{#each todo in todos}} {{#each tag in todo.tags}} <!-- in here, both todo , tag in scope --> {{/each}} {{/each}}
one option create separate template helper sorts elements in tags
array. sort elements in ascending order, use _.sortby(list, iteratee, [context])
, example:
if (meteor.isclient) { template.todos.helpers({ todos: function() { return todos.find(); }, sortedtags: function(todo) { return _.sortby(todo.tags, function(tag) { return tag; }); } }); } if (meteor.isserver) { meteor.startup(function() { if (todos.find().count() === 0) { todos.insert({ name: "homework", tags: ["school", "college", "university"] }); } }); }
<template name="todos"> {{#each todo in todos}} {{todo.name}} <ul> {{#each tag in sortedtags todo}} <li>{{tag}}</li> {{/each}} </ul> {{/each}} </template>
this can implemented sample data structure, provided in comments:
{ "gtext":"money", "owner":"qdqgdaxjahxnhx95u", "username":"prsz", "order":0, "tasks":[ { "taskname":"test subtask1", "taskorder":3 }, { "taskname":"test subtask2", "taskorder":1 } ] }
<template name="goals"> {{#each goal in goals}} {{goal.gtext}} <ul> {{#each task in sortedtasks goal}} <li>{{task.taskname}}</li> {{/each}} </ul> {{/each}} </template>
if (meteor.isclient) { template.goals.helpers({ goals: function() { return goals.find(); }, sortedtasks: function(goal) { return _.sortby(goal.tasks, function(task) { return task.taskorder; }); } }); }
here's meteorpad.
Comments
Post a Comment