循环显示对话框并对接受事件进行操作

Display dialogs in a loop and act on the accept event

在我的 Nativescript 应用程序中,我有一个循环,我想为每个被迭代的项目显示一个对话框。当对话框显示时,它包含 "Accept" 和 "Reject" 选项,单击这两个选项时我想调用一个方法,将迭代项传递给该方法。问题是因为选项选择 returns 承诺我失去了对迭代项目的引用。我该怎么做才能解决这个问题?这是我的代码示例。

编辑:我也真的不喜欢在 promise returns 之后在循环中声明一个函数。

function _showPendingConnections() {    
    for (var i = 0; i < ViewModel.pendingConnections.length; i++) {
        var pendingConnection = ViewModel.pendingConnections[i];
        dialog.confirm({
            message: pendingConnection.PatientFirstName + " would like to share their glucose readings with you.",
            okButtonText:"Accept",
            cancelButtonText:"Reject"                                    
        }).then(function(result) {
            if(result === true) {
                ViewModel.acceptConnection(pendingConnection);
            } else {
                ViewModel.removeConnection(pendingConnection);
            }            
        });
    }
}

以下更改对我有用(我可能创建了不同的 viewModel,但想法是一样的)- 我所做的只是在您的项目索引通过时进行更改。

例如:

// main-page.js

"use strict";
var main_view_model_1 = require("./main-view-model");
var dialogModule = require("ui/dialogs");
var viewModel = new main_view_model_1.MyViewModel();
viewModel.pendingConnections = [{ PatientFirstName: "John" }, { PatientFirstName: "Merry" }, { PatientFirstName: "Abygeil" }];
// Event handler for Page "navigatingTo" event attached in main-page.xml
function navigatingTo(args) {
    // Get the event sender
    var page = args.object;
    page.bindingContext = viewModel;
    for (var index = viewModel.pendingConnections.length - 1; index >= 0; index--) {
        connectionDealer(index);
    }
}
exports.navigatingTo = navigatingTo;
function connectionDealer(index) {
    var pendingConnection = viewModel.pendingConnections[index];
    dialogModule.confirm({
        message: pendingConnection["PatientFirstName"] + " would like to share their glucose readings with you.",
        okButtonText: "Accept",
        cancelButtonText: "Reject"
    }).then(function (result) {
        if (result === true) {
            // your code follow.. pass pendingConnection[index] to your method
            console.log("accepted by " + pendingConnection["PatientFirstName"]);
        }
        else {
            // your code follow.. pass pendingConnection[index] to your method
            console.log("Rejected by " + pendingConnection["PatientFirstName"]);
        }
    });
}


// main-view-model.js

   "use strict";
var observable = require("data/observable");
var MyViewModel = (function (_super) {
    __extends(MyViewModel, _super);
    function MyViewModel() {
        _super.apply(this, arguments);
    }
    Object.defineProperty(MyViewModel.prototype, "pendingConnections", {
        get: function () {
            return this._pendingConnections;
        },
        set: function (value) {
            if (this._pendingConnections !== value) {
                this._pendingConnections = value;
            }
        },
        enumerable: true,
        configurable: true
    });
    return MyViewModel;
}(observable.Observable));
exports.MyViewModel = MyViewModel;