@jessedvrs

Jesse de Vries

@jessedvrs

Jesse de Vries

Teaming platform for projects

Tips & tricks

@jessedvrs

for Meteor

@jessedvrs

Nested publications

Less reactivity

When a Meteor application gets big and serious

Collection helpers

You want all users from all part-ups...

@jessedvrs

What would you do?

Nested publications

@jessedvrs

// On the server

Meteor.publishComposite('partups.list', function() {
    return {
        find: function() {
            return Partups.find();
        },
        children: [
            {
                find: function(partup) {
                    return Meteor.users.find({
                        _id: {$in: partup.users}
                    });
                },
                children: [
                    // ... etc
                ]
            },
        ]
    };
});

reywood:publish-composite

Nested publications

@jessedvrs

// On the client

Template.blah.onCreated(function() {
    this.subscribe('partups.list');
});

Template.blah.helpers({
    partups: function() {
        return Partups.find();
    },

    partup_users: function() {
        var partup = this;
        return Meteor.users.find({
            _id: {$in: partup.users}
        });
    }
});

reywood:publish-composite

@jessedvrs

Meteor.users.find({
    _id: {$in: partup.users}
});

Collection helpers

@jessedvrs

// On "both"

Meteor.users.findForPartup = function(partup, options) {
    var selector = {_id: {
        $in: partup.users
    }};

    return this.find(selector, options);
};

@jessedvrs

// On the server

Meteor.publishComposite('partups.list', function() {
    return {
        find: Partups.find,
        children: [
            {
                find: Meteor.users.findForPartup,
                children: [
                    // ... etc
                ]
            },
        ]
    };
});

Collection helpers

in publication

@jessedvrs

// On the client

Template.blah.helpers({
    // ...

    partup_users: function() {
        var partup = this;
        return Meteor.users.findForPartup(partup);
    }
});

Collection helpers

in template helper

@jessedvrs

// On "both"

Meteor.users.guardedFind = function(userId, selector, options) {
    if (Meteor.isClient) return this.find(selector, options);

    //
    // make the selector more specific here
    // for example:
    //
    selector.friends = {
        $in: [userId]
    };

    return this.find(selector, options);
};

Collection helpers

for security

@jessedvrs

// On "both"

Meteor.users.findForPartup = function(partup, options, params) {
    params = params || {};
    var user_id = params.user_id || null;

    var selector = {_id: {
        $in: partup.users
    }};

    return this.guardedFind(user_id, selector, options);
};

Collection helpers

nested!

Less reactivity

@jessedvrs

// On client

var refreshed_at = new ReactiveVar(new Date());

Template.blah.onCreated(function() {
    this.subscribe('partup.one', this.data.partupId);
    this.subscribe('partup.updates', this.data.partupId);
});

Template.blah.helpers({
    updates: function() {
        var partup = Partups.findOne(this.partupId);

        return Updates.findForPartup(partup, {}, {
            lt_updated_at: refreshed_at.get()
        });
    }
});

Template.blah.events({
    'click [refresh-updates]': function() {
        refreshed_at.set(new Date());
    }
});

@jessedvrs

Less reactivity

adjusted collection helper

// On "both"

Updates.findForPartup = function(partup, options, params) {
    params = params || {};

    var selector = {
        partup_id: partup._id
    };

    if (params.lt_updated_at) {
        selector.updated_at = {
            $lt: params.lt_updated_at
        }
    }

    return this.find(selector, options);
};

@jessedvrs

Less reactivity

adding a counter

// On client

// ...

Template.blah.helpers({
    // ...

    new_count: function() {
        // ...

        Updates.findForPartup(partup, {}, {
            gt_updated_at: refreshed_at.get()
        }).count();
    }
});

// ...

@jessedvrs

Jesse de Vries

Thanks!

Useful tips & tricks for Meteor

By Jesse de Vries

Useful tips & tricks for Meteor

  • 1,052