0

データベース テーブルに対してクエリを実行するクリック イベントがあり、クエリが返すオブジェクトの長さに応じて異なるアラートをトリガーするはずです。このクエリはテーブルをチェックして、ユーザーが特定のオブジェクト ID に関連するユーザー情報を既に保存しているかどうかを確認します。id はイベントを表します。ユーザーが同じイベントの情報を 2 回保存できないようにしたい。そのため、リンクが初めてクリックされると、クエリはテーブルをチェックして、そのイベント ID に関連する情報が既に保存されているかどうかを確認します。保存されていない場合は、情報をイベント ID と共に保存します。しかし、そのリンクが 2 回目にクリックされたときに、情報を既に送信したことをユーザーに通知するメッセージを表示したいと考えています。どういうわけかテストすると、条件が2回目は機能しません。

$("#mainDiv").on('click', '.interested', function (event) {
    //get id attribute of the clicked "interested" link. It stores the id of the activity.
    var activityID = $(event.target).attr('id');

    /*instantiate object that will hold info about interested user. The constructor function and variables are declared in a different part of my code*/
    var intrstd = new interested(hood, gender, dateOfBirth, zip, uID);

    /*check the "Interested" table on Parse to see if the user already has info stored for this activity */
    var check = Parse.Object.extend("Interested");
    var interest = new Parse.Query(check);

    //these are query constrains that pick out the colum values I want. 
    interest.equalTo("parent", activityID);
    interest.equalTo("InterestedPPL", intrstd.uID);

    //interest.find() executes the query and "results" contains the return object
    interest.find({
        success: function (results) {
            /*if the user info is already saved for the specific id, show the alert, if not store the object containing their user info */
            if (results.length !== 0) {
                alert("you have already requested an invite to that activity");
            } else {
                var show = Parse.Object.extend("Interested");
                var int = new show();
                int.set("parent", activityID);
                int.set("InterestedPPL", intrstd);
                int.save();
                alert("you will be notified if you have been invited to this activity");
            } //closes else   
        },
        error: function (object, error) {
            // The object was not retrieved successfully.
            // error is a Parse.Error with an error code and description.
            console.log("Error: " + error.code + " " + error.message);
        }
    }); //closes find
}); //closes on
4

1 に答える 1

1

_clickFlagイベント ハンドラの外部で変数を宣言します。true_clickFlagの場合にイベントを処理します。クエリを実行する前に、falseに設定して、連続したクリックが処理されないようにします。クエリ成功のコールバックで、再度trueに設定します。この方法には利点があります。余分なクエリはまったく作成されません。私が十分に明確であることを願っています。以下のコードサンプルを参照してください。_clickFlag

var _clickFlag = true;
$("#mainDiv").on('click', '.interested', function(event){

    if (_clickFlag) {// Continue only if true

        _clickFlag = false; // this is to prevent other consecutive clicks to be processed when
        .
        .
        .

        interest.find({ success: function(results) {

            var show = Parse.Object.extend("Interested");
            var int = new show();
            int.set("parent", activityID);
            int.set("InterestedPPL", intrstd);
            int.save();

            _clickFlag = true;

            alert("you will be notified if you have been invited to this activity");
    .
    .
    .
    .
于 2013-07-02T02:33:40.423 に答える