devjs11
devjs11

Reputation: 1948

Titanium mobile, best way to get array value with custom loop

I am trying to figure out the best way to gey array value inside the the custom loop. Here is my code, I am not sure if that is a valid way of doing this or there is another way of doit it:

var win = Ti.UI.createWindow({ backgroundColor: '#fff', layout:'vertical' });

    var data = [
    {title:'Row 1',customValue:'123'},  
    {title:'Row 2',customValue:'345'},  
    {title:'Row 3',customValue:'234'},
    ];

    for(var i = 0, l = data.length; l--; i++) {

    thisObject = data[i];   

    var container = Titanium.UI.createView({
        left: 10,
        right: 10,  
        customValue:thisObject.customValue
    });

    var label = Ti.UI.createLabel({
        text : thisObject.title,        
        width : 'auto',
        height : 25     
    });

    container.add(label);
    win.add(container);
    container.addEventListener('touchend', function(e) {        
        alert(this.customValue);            
    });
    }

    win.open();

Thank you.

Upvotes: 0

Views: 2323

Answers (1)

Timothy Allyn Drake
Timothy Allyn Drake

Reputation: 926

Your solution is acceptable and is similar in concept to my approach. However, I would suggest you consistently use a unique property name for this custom data where necessary, and allow it to store many properties and their values by using an object. If in the future Appcelerator decides to create a property named customValue you may be contenting with the Titanium API and experience undesirable results.

Passing / storing your custom data:

var container = Titanium.UI.createView({
     left: 10,
     right: 10,
     myUniqueCustomDataObject: { customValue: thisObject.customValue }
});

Accessing your custom data object property like so:

container.addEventListener('touchend', function(e) {
     alert(this.myUniqueCustomDataObject.customValue);
});

Upvotes: 3

Related Questions