将结果从另一个控制器Titanium Alloy返回到控制器

时间:2014-11-05 17:23:35

标签: controller arguments titanium titanium-alloy

我正在使用Titanium Alloy MVC和需要扫描QR码的项目 Titanium SDK 3.4.0.GA

我有2个控制器:index.js和secondwindow.js及其各自的视图index.xml和secondwindow.xml。 我需要在secondWindow控制器中启动扫描并处理扫描结果,并将结果返回给索引控制器,让索引处理他的UI元素

我正在尝试这样的事情 INDEX.XML:

<Alloy>
  <Window>
   <Label id='result' />
   ...Other components...
   <Button onClick='startScan'>Start QR scan</Button>
  </Window>
</Alloy>

index.js:

function whenSecondWindowFinish(arg){
  //update index.xml
  $.result.setText(arg);
}

function startScan(e){
   Alloy.createController('secondWindow');
}

$.index.open();



secondWindow.xml:

<Alloy>
  <Window exitOnClose='false'>
  </Window>
</Alloy>

secondWindow.js:

function scanOK(data){
 var returnResult = /*Handle data*/
 //I need to return the result to the index controller
 $.secondWindow.close();//And close this view
}

function canceled(){
 //return {} to index controller
 $.secondWindow.close();//And close this view
}

var QRscanner = require('qrscanner');
var qroptions = {
  //width height ...
  success: scanOK,
  cancel: canceled
};
var qrview = QRscanner.createQRView(qroptions);
$.secondWindow.add(qrview);
$.secondWindow.open();


如何在成功/取消功能中关闭此窗口并将结果返回到索引控制器或通知索引执行whenSecondWindowFinish(/ 传递扫描结果的arg /);方法?或者哪种方法是正确的?

2 个答案:

答案 0 :(得分:1)

使用回调。

index.js:

var callbackFunc = function(data){
     //do something with the data variable
}
Alloy.createController('secondwindow', {'callback':callbackFunc});

secondwindow.js:

var args = arguments[0] || {};

function scanOK(data){
  args.callback(data)
 //I need to return the result to the index controller
 $.secondWindow.close();//And close this view
}

你也可以使用Ti.App.fireEvent来获得同样的东西,但这就是为什么你不应该:http://www.tidev.io/2014/09/10/the-case-against-ti-app-fireevent-2/(哦,那个链接也解释了回调:)

答案 1 :(得分:0)

使用这个: -

 activity.startActivityForResult(intent, function(e) {
    // The request code used to start this Activity
    var requestCode = e.requestCode;
    // The result code returned from the activity 
    // (http://developer.android.com/reference/android/app/Activity.html#StartingActivities)
    var resultCode = e.resultCode;
    // A Titanium.Android.Intent filled with data returned from the Activity
    var intent = e.intent;
    // The Activity the received the result
    var source = e.source;
});

OR

Have a look here

斯巴达克斯谢谢:)