在循环中显示对话框并对accept事件进行操作

时间:2016-06-27 01:54:29

标签: javascript nativescript

在我的Nativescript应用程序中,我有一个循环,并希望显示迭代的每个项目的对话框。当对话框显示它包含“Accept”和“Reject”选项时,两者都被点击时我想调用一个方法将迭代项传递给。问题是因为选项选择返回一个promise我丢失了对迭代项的引用。我该怎么做才能解决这个问题?这是我的代码示例。

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

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);
            }            
        });
    }
}

1 个答案:

答案 0 :(得分:1)

以下更改对我有用(我可能已经创建了不同的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;