2016-06-27 1 views
0

私のNativecriptアプリケーションではループがあり、繰り返し処理される各アイテムのダイアログを表示したいと考えています。ダイアログに "Accept"と "Reject"オプションが表示されたら、どちらもクリックして、反復アイテムを渡すメソッドを呼び出したいと思います。問題は、オプションの選択によって、反復アイテムへの参照が失われるという約束が返されるためです。これを回避するには何ができますか?ここに私のコードの例があります。ループ内のダイアログを表示して受け入れイベントに作用する

編集:約束が返ってからループの中で関数を宣言しているのも本当に好きではありません。

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

次の変更は(私はおそらく別の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; 
+0

私の場合は私は活字体の上にバニラJavascriptを使用しています。ビューモデルのGetとSetの表記法は同じですか? –

+1

サンプルをバニラJavaScriptコードに変更しました –

関連する問題