Unknow keys with Blaze What to do


#1

I have following stucture: How to i translate it into being displayed in blaze!
The entry keys are random!!!

{{#each entry}}

What here?
{{key.time}}
{{key.apple}}
?

{{//each}}

"entry" : {
    "randomkey1231x" : {
        "time" : 1436526422,
        "type" : "apple"
    },
    "random3da1231xdd" : {
        "time" : 1436526422,
        "type" : "tree"
    }
}

#2

I dont know if you dont have to prepare helper for that, but maybe something like

{{#each key in (Object.keys(this)) }}
  {{key.time}}
  {{key.type}}
{{/each}}

If we are talking about Meteor 1.2


#3

#each is used to iterate over a cursor or an array. Your example is neither - it’s an object. Is it possible to change the structure to a form which can be iterated over? Something like:

"entry" : [
  {
    "key" : "randomkey1231x",
     "time" : 1436526422,
     "type" : "apple"
   },
     "key" : "random3da1231xdd",
     "time" : 1436526422,
     "type" : "tree"
   }
]

#4

The data comes from a database cursor, if it makes a difference


#5
  1. In your helper that returns your data from the cursor you transform the data into a format that is much easier to iterate with Blaze.
  2. Iterate that and profit $$$.

e.g. using underscore and ES2015 I would write something like…

Template.abc.helpers({
  data () {
    let d = getMyDataFromDB() // might have to add .fetch() if it's really a cursor
    let ret = []
    _.each(d.entry, (val, key) => {
      ret.push(_.extend({}, val, {key})) // adding the original object value plus the random key available as "key"
    }
    return ret
  }
})

And in the template you can then just do…

{{#each data}}

time: {{time}}
type: {{type}}
(possibly more properties of each entry)
This is the "randomkey1231x" key thing: {{key}}

{{/each}}

#6

if going the transformation way, use native .forEach or .map of cursor to keep tracking dependencies