Sign in to follow this  
fnm

[web] javascript problem

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this