解析云-删除属性(指针) [英] Parse Cloud - delete property (pointer)

查看:79
本文介绍了解析云-删除属性(指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据模型由3个对象组成,其中两个(子代)使用指针链接到父代.

My data model consist of 3 objects, two of them (the children) are linked to the parent using a pointer.

MyModel是具有2个属性的父母:颜色和目标.两者都是指向其他对象的指针.

MyModel is the parent that has 2 properties: colors and goal. Both are pointers to other Objects.

当我删除父母时,我也希望删除孩子,但要注意的是,指针可能为零,因此在尝试删除之前,我需要检查那里是否有东西.

When i delete the parent I want the children to be deleted as well, the caveat is that the pointer might be nil, so I'd need to check if there is something there before attempting deletion.

[我是Java语言的新手,所以也许这也是问题的一部分]

[I'm new to Javascript so maybe that's also part of the problem]

Parse.Cloud.beforeDelete("MyModel", function(request) {
    if request.has(request.object.colors) {
      color = request.object.colors;
      Parse.Object.destroyAll(color, {
              success: function() {},
              error: function(error) {
                console.error("Error deleting related color " + error.code + ": " + error.message);
              }
            });
    }

    if request.has(request.object.goal) {
      goal = request.object.goal;
      Parse.Object.destroyAll(goal, {
          success: function() {},
          error: function(error) {
            console.error("Error deleting related goal " + error.code + ": " + error.message);
          }
        });
  }
});

推荐答案

让我们将其分解为较小的功能,并一路纠正OP代码中的几个问题.将事物简化为较小的,返回承诺的功能,这对保持代码模块化和并发性可管理非常有帮助.

Lets break this into smaller functions and correct a couple problems in the OP code along the way. It's very helpful to reduce things to smaller, promise-returning functions keep the code modular and the concurrency manageable.

编辑 通常,最好使用指针来关联对象.这是一个通用功能,用于删除通过指针关联的对象:

EDIT Generally, it's preferred to use pointers to relate objects. Here's a general purpose function to delete an object related via pointer:

function deleteRelatedPointer(myModel, pointerName) {
    var pointer = myModel.get(pointerName);
    if (pointer) {
        return pointer.fetch().then(function(relatedObject) {
            return relatedObject.destroy();
        });
    } else {
        return null;
    }
}

一些作者通过包含相关对象ID的字符串列来关联对象.这是删除与ID相关的对象的等效功能:

Some authors relate objects via a string column containing the id of the related object. Here's the equivalent function to delete an object related by id:

function deleteRelatedId(myModel, columnName, relatedClass) {
    var relatedId = myModel.get(columnName);
    if (relatedId) {
        var query = new Parse.Query(relatedClass);
        return query.get(relatedId).then(function(relatedObject) {
            return relatedObject.destroy();
        });
    } else {
        return null;
    }
}

现在,beforeDelete方法很容易编写和理解. (通过指针假定关系):

Now, the beforeDelete method is easy to write and understand. (Assuming the relationships via pointers):

Parse.Cloud.beforeDelete("MyModel", function(request, response) {
    var myModel = request.object;
    deleteRelatedPointer(myModel, "colors").then(function() {
        return deleteRelatedPointer(myModel , "goal");
    }).then(function() {
        response.success();
    }, function(error) {
        response.error(error);
    });
}

要注意的另一件重要事情是,before挂钩获取一个响应对象,并且在相关任务完成后,需要在对象上调用成功/错误.

The other important thing to notice is that the before hook takes a response object and is required to invoke success / error on that object after the related tasks are complete.

所有这些都取决于承诺,这​​是必要且非常有用的. 在此处了解有关parse的实现的信息.

All of this hinges on promises, which are necessary and immensely useful. Read about parse's implementation of them here.

这篇关于解析云-删除属性(指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆