• Advertisement
Sign in to follow this  

[web] javascript problem

This topic is 4293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Any javascript 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?

Share this post


Link to post
Share on other sites
Advertisement
Your problem is a basic javascript one. Essentially, it is sharing the same "gallery" variable from inside every event listener.

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 alert(this.id). 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.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement