sreenivas
sreenivas

Reputation: 2257

Run parse aftersave cloud code only on certain attributes

I set up the parse cloud code and its working great. I am looking for a way to run aftersave cloud code only if certain attributes are modified. Is it possible ?

Upvotes: 0

Views: 1128

Answers (3)

xnagyg
xnagyg

Reputation: 4931

My way:

Create a "dummy" column (not used in your app), named: changedcolumns

in beforeSave check each field, if it is dirty then put the changed field name to the changedcolumns field, like "name,date,gender"

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
        var changedColumns=",";
        if (request.object.dirty("locale")) {
            changedColumns=changedColumns+"locale,"
        }
        ....other columns....

        request.object.set("changedColumns",changedColumns);
        
        ....
        
});

in the afterSave you have a column with the changed column names (you can check it with indexOf function)

Parse.Cloud.afterSave(Parse.User, function(request) {
        var changedColumns=request.object.get("changedColumns");
        if (changedColumns==null) {
            changedColumns="";
        }
        if (changedColumns.indexOf(",locale,")>-1) {    //locale changed
            ... LOCALE CHANGED ...
            ... DO SOMETHING ...
        }
});

Upvotes: 1

knshn
knshn

Reputation: 3461

I am not sure what you want to do exactly, but you may use dirty or dirtyKeys to check if your attributes are modified in beforeSave.

In afterSave existed can be useful too to check if the object is just created or already existed.

Example for dirty:

Parse.Cloud.beforeSave("Location", function(request, response) {
  var location = request.object;

  if (location.dirty("photo")) {
    // photo is modified
    // do something (like resizing the photo as thumbnail)
  } 

  response.success();
  return;
});

Upvotes: 7

Wain
Wain

Reputation: 119031

No, it isn't possible. There is no such filter specified on the function. You can't differentiate first save / update in the function spec either. You will need to add logic inside the function to determine if it has any work to do.

Upvotes: 0

Related Questions