0

I am using a event normalizer for the mouse events and the touch events. That way, I don't need to maintain 2 pieces of code : one that responds to the touch events and the other one to the touch events.

It works fine, but I cannot have it to work with the live() function.

At the end of the normalization process, the normalized event is triggered this way:

var touchMouseEvent = normalizeEvent(type, event, event.pageX, event.pageY);
$(event.target).trigger(touchMouseEvent);

I tried to have the live event to work using those calls:

$('.MyClass').live('TouchMouseEvent', function(){...});
$('.MyClass').live('normalizeEvent', function(){...});
$('.MyClass').live('touchMouseEvent', function(){...});

but NOTHING WORKS.

Typically, the trigger for custom events are called as a string like $('.Class').trigger('customName'), so I wonder if this is why I am struggling with that.

Here is the code I using for the normalization. https://gist.github.com/2375726

Thanks!

Alexandre
  • 507
  • 1
  • 5
  • 16
  • 4
    Please use `.on()` as `.live()` is deprecated. – Drakkainen Jul 23 '12 at 17:45
  • Will do, but it was doing some test and I was using on() for those tests and it didn't work. – Alexandre Jul 23 '12 at 17:46
  • Custom events didn't work with `.live` in jQuery 1.3.x. What version of jQuery are you using? – Kevin B Jul 23 '12 at 17:49
  • I am using 1.7.2. I think live() was introduced in 1.4 so I should be OK. – Alexandre Jul 23 '12 at 17:50
  • Is `MyElement` dynamically created by any chance? – Drakkainen Jul 23 '12 at 17:57
  • `.live()` may be depreciated, but that's no reason for it to not work. If it isn't working with `.live`, switching to `.on` isn't going to automagically make it work. – Kevin B Jul 23 '12 at 18:00
  • yes, MyElement is dynamically created. This is why I was using the live() function so I don't have to bind the events everytime I have a new object created. – Alexandre Jul 23 '12 at 18:03
  • sorry, my example does not make sense, it should have read $('.myClass').live(...). since it the object with the ID does not work is not there when the page is ready, it will never be bind... At least I think... – Alexandre Jul 23 '12 at 18:05
  • @Alexandre You are using `.live()`, the element doesn't have to exist when the page is ready. That's the purpose of using it! – Kevin B Jul 23 '12 at 18:09
  • More than likely you just aren't binding to the correct event type. – Kevin B Jul 23 '12 at 18:12

2 Answers2

0

Not sure if live() will work on custom methods/events its based off delegation. Delegation itself relies on actual DOM events being fired, but they're delegated down to the document.body level. Try using bind() instead.

matsko
  • 21,895
  • 21
  • 102
  • 144
0

I found a solution. Instead of using the live() function, I use the delegate function.

For example:

$('#wayPointList').delegate('.deleteWayPoint', TouchMouseEvent.UP, function(){
//do something here
});

Works like a charm! Thanks everyone for your answer/suggestions.

Alexandre
  • 507
  • 1
  • 5
  • 16