ndemoreau
ndemoreau

Reputation: 3869

Best way to show a document field in Meteor?

If I have a collection with this kind of documents:

{
    "Category" : "DVDs",
    "items" : {
        "Name" : "Mission Impossible",
        "Owner" : "me",
        "LentTo" : "Alice"
    },
    "_id" : "4ocjZXCbLk5Zo8afp"
}

What's the easiest way to show "DVDs" in the browser.

I could do it by adding {{list_cat}} in a template or by creating this function:

Template.list.list_cat = function () {
  if (Session.equals('current_list', null)) return null;
  else {
    var cats = lists.findOne({ _id: Session.get('current_list')});
    return cats.Category;
  }
}

But I'm sure there is a faster cleaner way to do it?

Upvotes: 1

Views: 333

Answers (1)

Rahul
Rahul

Reputation: 12231

Given a template in which you show a list of documents, you can just refer directly to the property:

<body>
  {{> documents}}
  <br/><br/>
  {{> selected_doc}}
</body>

<template name="documents">
  {{#each document}}
  <div>{{Category}}</div>
  {{/each}}
</template>

<template name="selected_doc">
  Selected doc:<br/>
  Name: {{items.Name}}<br/>
  Owner: {{items.Owner}}<br/>
  Lent to: {{items.LentTo}}
</template>

Template.documents.events({
  "click div": function() {
    Session.set("selected_doc", this._id);
  }
});

Template.selected_doc = function() {
  return lists.findOne({_id: Session.get("selected_doc")});
}

Upvotes: 1

Related Questions