0

I've made this wonderful plugin: https://github.com/suprMax/Zepto-onPress

Which works perfectly besides one little detail. When I get callback function I need to store it alongside with my real event handler so I can detach it when someone tries to remove event handler and provide me the original callback. So basically I need to be able to store multiple key-value pairs per element where the key supposed to be a function and value is a function too. And I tried to do just that, but right now the script makes this internally:

(function(){}).toString()

Which is not a best idea since I can remove wrong event handlers because of :

(function(){}).toString() === (function(){}).toString(). 

I suppose there is a better way to do just that. Any suggestions are very welcome.

Brice Favre
  • 1,511
  • 1
  • 15
  • 34
Max
  • 1,149
  • 3
  • 10
  • 20

1 Answers1

0

The only safe and performant way is to store your functions in an array (indexOf works perfectly as it'll search on the pointer values) and use the array index to push the other method to another one.

var fn1store = [];
var fn2store = [];
function pushFunctions(fn1, fn2) {
   var p;
   if (p = fn1store.indexOf(fn1) > -1) {
     teardown(fn2store[p]);
   } 
   p = fn1store.push(fn1) -1;
   fn2store[p] = fn2;   

}
Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
  • Doesnt work. This way I loose relation to the node, you see. If the same event handler is associated with different nodes I'll get an error. – Max Apr 12 '12 at 20:02