0

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()];
  }
});    
Rob W
  • 341,306
  • 83
  • 791
  • 678
deluxe_247
  • 549
  • 2
  • 6
  • 14

2 Answers2

0

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
  • 5,994
  • 1
  • 18
  • 15
0

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
  • 1,129
  • 7
  • 10