开发者

passing arguments to addeventlistener in titanium

I require to pass arguments to callback handler of addEventListener. I tried this, but it doesn't work in for loop. Here is my code:

var view = Ti.UI.createView({
    //configuration here.
});
for(var i=0,ilen=response.length; i<ilen; i++){
    var thisObj = response[i];
    var btnCareer = Ti.UI.createButton({
        //configuration here
    });

    var careerID = thisObj.CareerID;
    btnCareer.addEventListener('click', function(){ 
                                                            bh.logic.career.CareerClick(care开发者_StackOverflow社区erID);
                                    });
    view.add(btnCareer);
}

What i get is the latest value.

Is there any way?


Without seeing what response[] contains and if careerID is intended to be linear, etc, the purpose of the following code is to suggest adding the careerID as an actual id property of the button; something which can be referenced.

I also do not know what Platform you are developing for ;-) so, give the following a whirl and if all else fails, check out:

The Button API by clicking here

HTH!!

var view = Ti.UI.createView({
    //configure the view here.
});

for(var i=0,ilen=response.length; i<ilen; i++){
    var thisObj = response[i];
    var careerID = thisObj.CareerID;
    var btnCareer = Ti.UI.createButton({
        //configuration here
        id:careerID // THIS GIVES THIS BUTTON AN ID TO MAP BY EVENT LISTENER ONCLICK
    });
    btnCareer.addEventListener('click', function(e){
        if ( e.source.id !== null ) {  // Be good and check for null values
            var careerIDValue = e.source.id;
            /* e is this function, source looks at id value for the button you just
             * created as it attaches the eventListener to the most current button
             * object and keeps the button unique because you've given it an id: blah
             * value that can be sussed out
             */ 
            bh.logic.career.CareerClick(careerIDValue);
        } else {
            alert('NULL ID FOUND!  ARGH! ' + e.source.id);
        }
    });
    view.add(btnCareer);
}


I finally got the answer here. Basically, i should remember that javascript is loosely coupled language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜