Search code examples
objecttitaniumundefinedtitanium-mobilecommonjs

Titanium mobile - Common JS. Objects values are lost when lauching a fireEvent


I'm dev an app into titanium mobile in javascript. The dynamic menu insert each new object(id,text,...., page) into a loop for (var x in tab).

with thoses items, specifics views are made.

var items = []; var menuIconsItem = require('view/module/menuIconsItem');

for(var i in itemTab) {
    var page = itemTab[i].page;

    items[i] = new menuIconsItem(itemTab[i]);

    menuFirstLine.add(items[i]);
    (function(itemsEvent) {
        itemsEvent.addEventListener('click', function() {

            Ti.App.fireEvent('test' +i, {
                id : i
            });
        })
    })(items[i]);

}

on the other controller side, i only get the last id reference.

If i = 0 to 5, i only get the last reference. The rest is undefined.

How could i do please?


Solution

  • First you have to set id for your menuIconsItem, I am taking button an an example here.

    items[i] = Titanium.UI.createButton({
            id:"button_"+i,
            _index: i
    })
    

    Then do this:

    (function(itemsEvent) {
           itemsEvent.addEventListener('click', function(e) {
    
               alert(e.source.id);
           })
     })(items[i]);