Skip to content Skip to sidebar Skip to footer

Calling A Function Without Passing Arguments Meteor App

Working through discover meteor (page 92), one section covers this code: Posts.allow({ update: ownsDocument, remove: ownsDocument }); ownsDocument = function(userId, doc)

Solution 1:

Meteor automatically passes the userId and doc arguments to whatever function object you assign to the update and remove keys passed to allow. The code you posted works the same as:

Posts.allow({
    update: function(userId, doc) {
        return doc && doc.userId === userId;
    },
    remove: function(userId, doc) {
        return doc && doc.userId === userId;
    }
});

The ownsDocument function doesn't get called in your code. The allow function informs Meteor to call that function any time a Post is updated or removed.

Solution 2:

[...] when it doesn't appear Posts.allow is passing the userId/doc when it's being called?

And where does it get called?

In your example, ownsDocument is not called at all. All you are doing is configuring which functions should be called in the update and remove case.

The functions are called somewhere else and at a different time, and there they will be passed the right arguments.


It's just like with event handlers. foo.onclick = bar; doesn't call bar. It assigns a reference to function to foo.onclick, so that the function can be called some time later. And at that moment, the function will be passed an event object.

Post a Comment for "Calling A Function Without Passing Arguments Meteor App"