在解析计数器中将限制设置为零 [英] Set Limit to Zero in Parse Counter

查看:121
本文介绍了在解析计数器中将限制设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将计数器的解析度设置为在分数递减时不低于零,此刻它可以变为负数.如何将最小限制设置为零?

I'm trying to set my counter in parse to not go below zero when the score is being decremented, at the moment it can go to negative numbers. How can I set the minimum limit to be zero?

这是我到目前为止要做的:

This is what I've managed to do so far:

btnPointTeamD.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                object.increment("team_d_score");
                                object.saveInBackground();
                            }
                        });

btnMinusTeamC.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                object.increment("team_c_score", -1);
                                object.saveInBackground();
                            }
                        });

推荐答案

云代码具有所谓的beforeSaveafterSave触发器. beforeSave是您这里需要的.

Cloud code has what's called beforeSave and afterSave triggers. beforeSave is what you need here.

beforeSave触发器包含所有新数据(注意:没有旧数据),您可以检查object.dirty("key");以查看该字段是否已更改.您也不必对此特定情况进行任何检查.

A beforeSave trigger contains all of the new data (note: none of the old) and you can check object.dirty("key"); to see if that field has changed. You also don't have to do any checks for this specific case.

Parse.Cloud.beforeSave("ClassName", function(request, response) {
    var object = request.params.object;
    if( object.get("team_c_score") < 0 ) object.set("team_c_score", 0);
    response.success();
});

一些注意事项:如果返回response.error(),保存将通过 note 进行,因此这是验证输入的方法.一个字段包含非法字符或您没有想到的数据?引发错误,因此无法保存.

Some notes: If you return response.error(), the save will note go through, so this is how you validate input. A field contains illegal characters, or data you didn't expect? Throw an error so it doesn't get saved.

您也不应在成功响应中添加任何内容.那会导致错误.

You also shouldn't put anything in the success response. That will cause an error.

此函数(如果存在)将自动调用,并且将始终被调用.你不能跳过它.即使您从仪表板更新数据,也会调用它.与afterSave触发器相同,尽管除非您保存其中的对象,否则在其中修改对象将不会做任何事情.

This function gets called automatically if it exists, and will always be called. You can't skip it. Even if you update data from the dashboard, this gets called. Same with afterSave triggers, although modifying an object in those will not do anything unless you save it.

这应该放在您的main.js或main.js所需的文件中.我的每个自定义类都有一个文件夹.每个类都有一个classNameController.js,其中包含beforeSave,afterSave,initializer和与该对象直接相关的任何云代码功能.

This should go in your main.js, or a file required by main.js. I have a folder for each of my custom classes. Each class has a classNameController.js, which contains the beforeSave, afterSave, initializer, and any cloud code functions relating directly to that object.

Main需要这些控制器中的每一个,这将向服务器打开所有Parse.Cloud端点.

Main requires each of these controllers, which opens up all of the Parse.Cloud endpoints to the server.

beforeSave和afterSave触发器有3秒钟的超时.我不知道解析服务器是否超时.我从来没有测试过.但是为了安全起见,最多只能进行几次服务器呼叫.

beforeSave and afterSave triggers on hosted Parse.com had a 3 second timeout. I am not aware if there is a timeout for parse-server. I've never tested it. But don't have more than a couple server calls to be safe.

这篇关于在解析计数器中将限制设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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