2016-06-27 4 views
0

내 Nativescript 앱에서 루프가 있고 반복되는 각 항목에 대한 대화 상자를 표시하려고합니다. 대화 상자에 "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

- 내가 행한 모든 당신의 품목 지수가 전달 될 때 변경하는 것입니다. 예를 들어

:

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

내 경우에는 내가 타이프 라이터 바닐라 자바 ​​스크립트를 사용하고 있습니다. 뷰 모델에서 Get 및 Set 표기법이 동일합니까? –

+1

샘플을 바닐라 JavaScript 코드로 변경했습니다. –

관련 문제