Set boolean on object change Parse.com

时间:2015-10-29 15:56:41

标签: javascript android parse-platform cloud-code

Situation:

I have a database at Parse.com. In this database I have objects which have an Array and a boolean.

Wanted Result:

I want the boolean to be set to true if the Array length is bigger than 0. And false if the size is 0.

Problem:

I do not want to set this boolean on adding/deleting from array, but i want some kind of listener that reacts on a change to the object.

A must

This solution has to be possible with only cloud code.


Is there any way to do this?

1 个答案:

答案 0 :(得分:3)

绝对可以使用cloud code beforeSave hook轻松完成。

在所需类的beforeSave中,查看数组的大小并相应地设置bool。这将保证bool每次保存都是最新的。

以下是一些示例代码

// Update the boolean based on the array length
Parse.Cloud.beforeSave("yourClass", function(request, response) {

    var yourArray = request.object.get("yourArray");

    if (yourArray.length > 0) {
        request.object.set("booleanProperty", true);
    } else {
        request.object.set("booleanProperty", false);
    }
    response.success();
});