简体   繁体   中英

grabbing individual document in meteor template helper

I am trying to access the individual document within a #each in Meteor so that I can mark it. Basically if the user clicks on the content I want to run some logic on that document. Example:

{{#each document}}
<div>
  {{text}}
  {{did_i_read_the_doc}}
</div>
{{/each}}

and I want the {{did_i_read_the_doc}} to show "yes" or "no" depending on whether it is marked in the collection. So in my template helper I want something like

Template.documents.helpers({
  did_i_read_the_doc: function() {
    //what goes here?
    if (thisIndividualDocument.contains(Meteor.userId() in "readBy")
      show "yes" in html
    else
      show "no" in html
  }

I'm not sure how to access the individual mongo document in the helpers wrapped in an each block. I'm used to the logic of returning all of the documents in a helper documents: function() { return Documents.find({}) }

but how would I define a helper function that gets the individual document? In the final run of things I want to be able to click a document and mark it as read, like a notification, and have something that shows up saying "already read" or "unread" depending.

I figured this out right after I posted.

In the helper method you can use

my: function() {
  if(this.field)
    //do something

so 'this' grabs the individual document.

EDIT: just for example to solve my specific case

did_i_read_the_doc: function() {
  if(this.read) {
    return "yes"
  }
  else {
    return "no"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM