使用Parse Cloud插入镜像对象

时间:2015-02-21 23:34:13

标签: android parse-platform nosql

我有一个场景,我需要复制一个对象(在保存到Parse之前或之后)只更改一个字段。 问题是,我无法弄清楚如何在保存这个新对象时找到停止条件,并且一次又一次地调用afterSave回调。 我的目标:

{
        "createdAt": "2015-02-21T23:25:03.525Z",
        "creator": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "2k9OzzBrPr"
        },
        "date": {
            "__type": "Date",
            "iso": "2015-02-21T22:46:39.048Z"
        },
        "description": "Hdheha",
        "from": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "Sd9B1XyZVL"
        },
        "has_accepted": false,
        "has_answered": false,
        "objectId": "YQCWRo0j2V",
        "status": 0,
        "to": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "2k9OzzBrPr"
        },
        "updatedAt": "2015-02-21T23:25:03.525Z",
        "value": 2.3499999046325684
    }

我的(尝试)服务器代码:

function saveMirrorDebit(request) {
    var toUser = request.object.get("to");
    var fromUser = request.object.get("from");
    var invertedDebit = request.object;

    var Debit = Parse.Object.extend("Debit");
    var query = new Parse.Query(Debit);
    query.equalTo("parent_debit", {
        __type : "Pointer",
        className : "Debit",
        objectId : invertedDebit.id
    });

    query.find({
        success : function (debit) {
            if (debit) {
                console.log('debito nao nulo');
            } else {
                console.log('debito nulo, criando o espelho invertido');

                var newDebit = new Debit();

                newDebit.set("creator", invertedDebit.get("creator"));
                newDebit.set("from", toUser);
                newDebit.set("to", fromUser);
                newDebit.set("value", -invertedDebit.get("value"));
                newDebit.set("parent_debit", {
                    __type : "Pointer",
                    className : "Debit",
                    objectId : invertedDebit.id
                });

                newDebit.save(null);
            }
        },
        error : function (error) {
            console.log(error);
        }
    });
}

在afterSave上调用:

Parse.Cloud.afterSave("Debit", function (request) {
...
saveMirrorDebit(request);
...
}

我该如何处理?

由于

1 个答案:

答案 0 :(得分:1)

首先,我不建议你复制任何对象......你想要实现什么目标?

无论如何,在afterSave中,你可以实现你想要的。请注意,beforeSave可以为您节省一次API调用。

Parse.Cloud.afterSave("Debit", function (request) {

    var debit = request.object;
    if (!(debit.has("duplicated"))){
        var Debit = Parse.Object.extend("Debit");
        var duplicated_debit = new Debit();
        duplicated_debit.set("creator", debit.get("creator"));
        duplicated_debit.set("date", debit.get("date"));
        duplicated_debit.set("from", debit.get("from"));
        // repeat as many times as needed, include your "change logic here"

        // that's where the magic happens
        duplicated_debit.set("duplicated",true);
        debit.set("duplicated",true);
        Parse.Object.saveAll([duplicated_debit,debit]);
    }
}