0

I have a small problem using private variables in a JavaScript function. This software depends on draw2d, a rendering library for javascript. The library forces you to use John Resig's implementation of Class.js, which allows for simple Java like class inheritance, and what i believe is the root of the problem.

//= require block/XBlock.js
console.log("sblock loaded.");
xmicro.block.SimpleBlock = xmicro.block.XBlock.extend({
    NAME: "xmicro.block.SimpleBlock",

    init: function(path, width, height,input,output,contextMenua){
        this._super(path,width,height,input,output,contextMenua);
        image = path;
        this.contextMenu = contextMenua;
        xmicro.istn.BlockAttributeStore[this.id] = new xmicro.block.BlockAttributeStore();
    },
    getImage: function () {
        return this.image;
    },

    onContextMenu: function (x, y) {
        supaId = this.id;
        that = this;
        var buildFunc = function(){
            var args = Array.prototype.slice.call(arguments);
            args.push(supaId);
            return that.contextMenu.build.apply(null, args);    
        };
        if(this.contextMenu !== undefined){
            $.contextMenu({
                trigger: "right",
                selector: this.contextMenu.selector,
                build: buildFunc
            });
        }
    },

    getPersistentAttributes: function () {
        var memento = this._super();

        return memento;
    }
});

The comment at the top does some auto concatenation, but the focus of the problem is at the onContextMenu function. I have a closure there that needs to be called in order to build a jQuery context menu from a properties object. One thing inside that properties object is a method called build, for which I use buildFunc to intercept the call and append the id of the figure it was called on.

The problem is that if I declare var supaId = this.id, as I make more figures of this type, they start sharing supaId, and start cross-contaminating variables. The other problem is that if I use just supaId = this.id like I below, then it works, but puts supaId on the global scope. Neither of these are good options for me, and I would like to understand how to fix it.

Any help is appreciated!

Reliant
  • 76
  • 4
  • John Resig's `Class` is by no means forced. It just adds syntactic sugar, you are able to do inheritance the "normal" way as well. – Bergi Jun 14 '14 at 15:07
  • No, `var supaId` is correct. Does only one of the `buildFuncs` you created get triggered if they "share" it? There must be something else wrong. – Bergi Jun 14 '14 at 15:11
  • `image = path;` should probably be `this.image = path;` You don't want a global `image` variable there. – Bergi Jun 14 '14 at 15:12
  • Yeah @Bergi, that is a fragment of something I was using earlier. onContextMenu is triggered when a instance of a figure is right clicked on, so it runs that instances specific onContextMneu code, which in turn calls buildFunc. I don't believe it gets triggered more than once. – Reliant Jun 14 '14 at 15:23
  • @Bergi, just confirmed `buildFunc` is ran once per click. – Reliant Jun 14 '14 at 15:26
  • Nah, I wanted to ask whether the *wrong* `buildFunc` is run - you say you have multiple instances. Does each of the `buildFunc`s run once? – Bergi Jun 14 '14 at 15:28
  • @Bergi Oh! No, when i say multiple instances, I mean multiple instances of an entire figure, in other words this entire file. From what I can tell, one figures `onContextMenu` does not call the `buildFunc` of another. – Reliant Jun 14 '14 at 15:30
  • So what makes you think then that they shared `supaid` when you declare it as a local variable with `var`? – Bergi Jun 14 '14 at 15:31
  • @Bergi When I open the contextMenu, it retrieves the last saved values, then when closed saves them. Ill do it to one figure, then all of the figures have that same value. Also, when I stepped through it in the debugger, i kept seeing the id of the figure change, as it started for some reason referencing other figures. – Reliant Jun 14 '14 at 15:37

0 Answers0