Skip to main content
gamedev.net
🔒 Locked

[web] javascript problem

Started by fnm 0 replies 700+ views
Original Post
fnm
fnm
Any &#106avascript guru that can have a look at the folowing code?

  window.application.main = function(){
    var d = this.getClientWindow().getClientDocument();
    var bs = new QxBarView;
    for(var i=[0];i<[3];i++){
       var bvb = new QxBarViewButton("page"+i, [0]);
       bs.getBar().add(bvb);
       var p = new QxBarViewPage(bvb);
       var galleryList = [];
       galleryList.push({
        width : [350],
        height : [350],
        thumbWidth : [64],
        thumbHeight : [64],
        comment : "comment",
        title : "title",
        src : "pic.gif"
      });

    var gallery = new QxGallery(galleryList);
    gallery.id=i;
    gallery.addEventListener("click", function(e){
       alert(gallery.id);
    });

    p.add(gallery);
    bs.getPane().add(p);
    }
    d.add(bs);
}

it uses qooxdoo. basically, what this code is doing is creating 3 galleries and adding 3 event handlers to them; whenever i click a gallery, i expect to get a message box containing the id of that gallery. however, no matter what gallery i click, i get the last added gallery id, 2. i have the feeling that this isnt a qooxdoo lib issue, but my bad understanding of how js works. i would expect that clicking the 1st gallery to see 0, the 2nd 1 and so on. anyone that can shed some light on this?
konForce
konForce
Your problem is a basic &#106avascript one. Essentially, it is sharing the same "gallery" variable from inside every event listener.<br/><br/>I'm not familiar with qooxdoo, but perhaps they have a built in method for getting around this? Ideally you would use the "this" pointer inside your function like <tt>alert(this.id)</tt>. It may already do the magic required to make that work. (Typically, the "this" pointer will be a reference to the HTML object in the case of an event, but you can override that with some trickery.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.