0
votes

I am using Titanium Alloy MVC with a project that needs to scan QR code Titanium SDK 3.4.0.GA

I have 2 controllers: index.js and secondwindow.js with their respective views index.xml and secondwindow.xml. I need to start the scan and handle the result of the scan in secondWindow controller, and return that result to the index controller, to let index handle his UI elements

Im trying something like this 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();


How can I close this window in the success/cancel functions and return the result to the index controller or notify index to execute whenSecondWindowFinish(/pass arg of scan result/); method? Or which is the correct way to do that?

2
what i understand is you want to send some result from secondWindow.js to index.js ( am i correct ?) and what is the type of result you want to send ( String , object , other ) ? - turtle

2 Answers

1
votes

Use a callback.

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
}

You could also use a Ti.App.fireEvent to get the same thing but here's why you shouldn't: http://www.tidev.io/2014/09/10/the-case-against-ti-app-fireevent-2/ (oh that link also explains Callbacks too :)

0
votes

Use this one:-

 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

Spartacus Thanks :)