简体   繁体   English

循环显示对话框并处理accept事件

[英]Display dialogs in a loop and act on the accept event

In my Nativescript app I have a loop and want to display a dialog for each item being iterated over. 在我的Nativescript应用中,我有一个循环,想为每个要迭代的项目显示一个对话框。 When the dialog displays it contains "Accept" and "Reject" options, both of which when clicked I would like to call a method which I pass the iterated item into. 当对话框显示时,它包含“接受”和“拒绝”选项,当单击它们时,我都想调用将迭代项传递到其中的方法。 The issue is since the option selection returns a promise I lose the reference to the iterated item. 问题在于,由于选项选择返回了一个承诺,我将丢失对迭代项的引用。 What can I do to get around this? 我该怎么办才能解决这个问题? Here's an example of my code. 这是我的代码示例。

EDIT: I also really don't like that I'm declaring a function in the loop after the 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);
            }            
        });
    }
}

The following change worked for me (I have probably created different viewModel but however the idea is the same) - all I have done is to change when your item index is passed. 以下更改对我有用(我可能创建了不同的viewModel,但是想法是相同的)-我所做的就是更改传递项目索引时的更改。

For example: 例如:

// 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM