1
votes

I fear that I know the answer to this, but I'll throw it out here. Is there any way of getting a beforeSave method to trigger on all Parse.Object subclasses using Cloud Code?

So I want to run some code on all saves, not any specific subclass of Parse's Object.

I've tried this:

Parse.Cloud.beforeSave(Parse.Object, function(request, response) {
}

... but it doesn't seem to get triggered.

3

3 Answers

3
votes

I have never heard of this, and, frankly, don't see much benefit. That first parameter is supposed to match a class name, so I doubt there is a work around for this out of the box.

You really should have all separate beforeSave's, though. We create a separate folder within our cloud code for each class, and give each class a Controller (beforeSave, afterSave, cloud functions related directly to the class) which has cloud functions that call other Repos which handle logic for different classes. The Controller is the only file that should touch the Repos from other classes.

Then there is the Repo for each class, which handles doing a specific task / logic, and often saves or fetches objects.

Then there are Factory files, which do no saves, no fetches, no extra calls. They ONLY perform business logic with available data passed in.

Every time you make a new Parse Class, I'd get in the habit of setting up that controller and you can add a beforeSave trigger there.

0
votes

No you cannot. You only can make triggers to user defined classes and predefined classes. if the class is user defined, first parameter must be a string e.g.

Parse.Cloud.beforeSave("Car", function(request, response) {
...
}

if the class is predefined, first parameter must Parse.ClassName e.g.

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
...
}
0
votes

You could first get all class names querying the schema (see this post) and then use a forloop to add to each one the beforeSave hook. In pseudo code:

var classNames = getClassNamesFromSchemaQuery();
classNames.foreach(var classObject){
    Parse.Cloud.beforeSave(classObject.className, (req, resp){
        // resto of the code here
    })
}