How do I display all document attributes


#1

Other than using a specialized autoform package, is there a way to make a template display all document attributes, even those added recently tha might be present in some documents only, and not in others?

(In other words: I would like to make sure, in a specific template, to see everything in a document, whatever was added to it that was not yet addressed in template code)


#2

Something like this?

    <ul>
    {{#each objects}}
        <li>
        {{#each getObjectAttributes}}
            {{getKey}}: {{getValue}}
        {{/each}}
        </li>
    {{/each}}
    </ul>
  Template.foo.helpers({
    getObjectAttributes: function () {
      return _.pairs(this);
    },
    getKey: function () {
      return this[0];
    },
    getValue: function () {
      return this[1];
    }
  });

#3

you can do whatever you like. the document is just json. what’s the use case?


#4

Thanks, I will try it later when I will reach that step. You proved that it is speaking a language we learn to speak it. I took a look at your code, and investigated _.pairs and all Underscore. Thanks.

I will reply in the same thread to maxhodges for telling the use case.


#5

Maxhodges,

Of course, one use case is to define an autoform (I did not investigate behond API the autoform package) that is not limited in anyway, or that is customizable.

Also, the problem is that any document can potentially have new attributes not present in previously created or updated documents. Unless we use a schema management, I think, especially for site managers, it would be good to make sure our display interfaces include all available attributes, so we know they exist, even if not stated in the template.
Even if we use schema management controling writes/updates on the collection, remains the problem that templates are maybe not up to date. So, we can have a feature that already display any new attributes in all templates (admin side).
(Especially in dev stage)

I suppose it can be a good way to notice we need to add it to the actual template.
Next step (after some code such as the one posted by hellstad in this thread), we need to code a function that would scan the template and the document and list the missing attributes in the template, so we can display them somewhere in the template without duplicate. Any “howto” welcome!


#6

I don’t really understand your use-case. A document is just a record of something. you can store different types of documents I the same collection but better to group related records together in collections and enforce a schema to keep things consistent.

Fir example you might have a Books collection for records about books. Attributes may included title, date published, number of pages, authors, price, etc.

if you need to add attributes later, like a ‘category’ field, you’d usually need to update your templates because your need to decide how you are going to display ‘category’ I.e. where on the page, what CSS class to use, etc.

for one-to-many relations, like authors, you can put them in an array in your document. Then iterate using #each in your template, to output them in a table or list for example.

If you can be very specific about your concern maybe we can be more helpful. My company has built a very large ecommerce system in Meteor with lots of complex documents. I highly recommend autoform and simpleSchema packages for projects of any size.