JSFiddle - React, Tailwind, and code Playground

by Billybobbonnet

JavaScript

var self = this;
var initializing = true;
myCollection
    .find() //unfiltered find because we need to observe all the fields
    .observeChanges({
        changed: function (id, field) {
            var raw_document = myCollection.findOne({
                _id: id
            });
            switch (field) {
                case "item":
                    if (!initializing){
                        self.changed("myCollection", id, {
                             item_count: raw_document.item.length;
                        });
                    }
                    break;
                default:
            }
        }
    })
    .forEach(function (entry) { //I iterate on each document in order to add its item_count field
        self.added("item_count", entry._id, entry.item.length);
    })
    .find({}, {item: 0}) // I filter my item array

//remove the initialization flag
initializing = false;
// Stop observing the cursor when client unsubs.
// Stopping a subscription automatically takes
// care of sending the client any removed messages.
self.onStop(function () {
    handle.stop();
}});
// I update the values in case a change has been observed
self.ready();`