views:

77

answers:

2

Hi-

I'm creating a new instance of the 'Draggable' object (from the script.aculo.us dragdrop module) inside a function belonging to an object I created. Let's call that object the 'Person' object. Easy enough. However, from inside the 'onEnd' function within the Draggable object, I need to call another function, getCell(), also belonging to the aforementioned 'Person' object. Kind of circulatory, I know, but I wish I knew how to do this. Nothing I've tried works. Can it be done? Below is an example heavily edited for brevity but I hope it conveys my intent. I'd be grateful for any insights here. Thanks.

var Person = Class.create({
  initialize: function(name) {
    this.name = name;
  },        
  move: function(p) {
    p = new Draggable(p, {     
     onEnd: function(d) {      
      var pos = getCell(d.element);
      .......
      .......
     }
     .......
     .......
    });
  },
  getCell: function(t) {
    var pos = t.positionedOffset();
    return [(pos.left / 64).floor(), (pos.top /64).floor()];
  }
});
A: 

try

move: function(p) {
    p = new Draggable(p, {     
     onEnd: function(d) {      
      var pos = p.getCell(d.element);
      .......
      .......
     }
     .......
     .......
    });
  },

or

move: function(p) {
    var getCell = function (el) {
        return p.getCell ( el );
    };

    p = new Draggable(p, {     
     onEnd: function(d) {      
      var pos = getCell(d.element);
      .......
      .......
     }
     .......
     .......
    });
  },
Jamie
A: 

try Function.bind:

var Person = Class.create({
  initialize: function(name) {
    this.name = name;
  },        
  move: function(p) {
    p = new Draggable(p, {     
     onEnd: function(d) {      
      var pos = this.getCell(d.element);
      .......
      .......
     }.bind(this)
     .......
     .......
    });
  },
  getCell: function(t) {
    var pos = t.positionedOffset();
    return [(pos.left / 64).floor(), (pos.top /64).floor()];
  }
});
Bird