Can not use Variables when I use Ti.include on a Eventlistener and onload-Function

You must Login before you can answer or comment on any questions.

Hello,

i can not use a Var in a Eventlistener and onload-function. I get an Application Error:

Can`t find variable: eintrag at parse-json-suchen.js

Here is my Code

search_leutesuchen.addEventListener('change', function(e){
 
    var url = "http://www.urltosomething.com/suche.php";
    var tableData_leutesuchen = [];
    var json, eintraege, eintrag, i, row;
 
    var xhr = Titanium.Network.createHTTPClient({
        onload: function(e) {
 
            Ti.API.info('response: '+ this.responseText);
            json = JSON.parse(this.responseText);
            for (i = 0; i < json.eintraege.length; i++) {
 
                eintrag = json.eintraege[i];
 
                Ti.include('lib/parse-json-suchen.js');
 
                tableData_leutesuchen.push(row);
 
            }
 
        table_leutesuchen.setData(tableData_leutesuchen);            
 
        },
        onerror: function(e) {
            L('error');
        },
        timeout:10000
    });
 
    var params = {  
        'uid': Titanium.Platform.id,  
        'query': e.value,
        'task': 'sucheleutenick'
    }; 
 
    xhr.open("POST", url);
    xhr.send(params);
 
});
And my Code on the included-file (parse-json-suchen.js)
row = Ti.UI.createTableViewRow({
                    height:'80dp',
                    backgroundColor: 'transparent',
                    hasChild: true, 
                    test:'profil.js'
                });
 
                nameLabel = Ti.UI.createLabel({
                        text: eintrag.name,
                        font:{
                            fontSize:'20dp',
                            fontWeight:'bold'
                    },
                    height:'auto',
                    left: 80,
                    top:'15',
                    color:'#000',
                    touchEnabled:false
                });
 
                if(eintrag.geschlecht == 'w'){ var sex = 'weiblich'; }
                if(eintrag.geschlecht == 'm'){ var sex = 'männlich'; }
 
                 sloganLabel = Ti.UI.createLabel({
                        text: sex + ', ' + eintrag.alter,
                        font:{
                            fontSize:'16dp'
                    },
                    height:'auto',
                    left: 80,
                    top:'45',
                    color:'#000',
                    touchEnabled:false
                });
 
 
                if(eintrag.mbild == ''){
                    if(eintrag.geschlecht == 'w'){ var bildi = 'icons/frau.png'; }
                    if(eintrag.geschlecht == 'm'){ var bildi = 'icons/mann.png'; }
                } else {
                    var bildi = "http://www.someurl.com/mitglied/" + eintrag.mid + "/" + eintrag.mbild;
                }
                //Ti.API.info(bildi);
                var bild =  Titanium.UI.createImageView({
                        image: bildi
                });
 
                if(eintrag.mbild == ''){
 
                    bild.left = 15;
                    bild.top = 25;
                    bild.width = 24; //wichtig für Android
 
                } else {
 
                    bild.left = 10;
                    bild.top = 10;
 
                    bild.width = 50;
                    bild.height = 50; 
                }
 
                row.add(bild);
 
 
 
                //Alle Daten von jSON
                row.mid = eintrag.mid;
                row.name = eintrag.name;
                row.mbild = eintrag.mbild;
                row.bezstatus = eintrag.bezstatus;
                row.uebermich = eintrag.uebermich;
                row.alter = eintrag.alter;
                row.geschlecht = sex;
 
 
                row.add(nameLabel);
                row.add(sloganLabel);

When I dont include all works fine. When I dont use a eventListener with a onload-function it works fine, too.

Any hints or solutions?

Cheers Fabian

— asked 8 months ago by Fabian Rodler
1 Comment
  • untested, but try to include the parse-json-suchen.js only once and wrap everything in that file into a function. In the for loop just call the function - like:

    var xhr = Titanium.Network.createHTTPClient({
            onload: function(e) {
     
                Ti.API.info('response: '+ this.responseText);
                json = JSON.parse(this.responseText);
     
                Ti.include('lib/parse-json-suchen.js');
     
     
               for (i = 0; i < json.eintraege.length; i++) {
     
                    eintrag = json.eintraege[i];
     
                    var row=myFunction(eintrag);
     
                    tableData_leutesuchen.push(row);
     
                }
     
    ...
    ...
    and in your parse-json-suchen.js wrap everything in a function called myFunction:
    function myFunction(eintrag){
     
        //your complete code here
     
        return row;
    }

    — commented 8 months ago by Soeren Damrau

1 Answer

I have found a solution by myself. When i put this hole code above in an includes-file it works!

var tableData_leutesuchen = [];
    var json, eintraege, eintrag, i, row;
 
    var xhr = Titanium.Network.createHTTPClient({
        onload: function(e) {
 
            Ti.API.info('response: '+ this.responseText);
            json = JSON.parse(this.responseText);
            for (i = 0; i < json.eintraege.length; i++) {
 
                eintrag = json.eintraege[i];
 
               // etc bla bla ...
               // etc bla bla ...
               // etc bla bla ...
               // etc bla bla ...
               // etc bla bla ...
               // etc bla bla ...
 
 
 
                tableData_leutesuchen.push(row);
 
            }
 
        table_leutesuchen.setData(tableData_leutesuchen);            
 
        },
        onerror: function(e) {
            L('error');
        },
        timeout:10000
    });

— answered 8 months ago by Fabian Rodler
answer permalink
1 Comment
  • I would strongly suggest that you look at some of the demo apps, ie: KitchenSink, to see how application code is structured. I can tell just by looking at how you had the code structured, that you may run into more issues like that down the road. Breaking apart the code like you had it, and doing a Ti.include() in a for loop, made no logical sense. I'm glad you found your answer, but make sure you do your due diligence to save yourself from future headaches.

    — commented 8 months ago by Anthony Decena

Your Answer

Think you can help? Login to answer this question!