1

I have a list of items on a mobile app (iPhone), and I have a "more" button that adds more list items. I catch the touch event on the "more" button, add more items to the list, and then the item that appears where the "more" button was is immediately triggered!

Template.more.events({
    'touchend li[name=more]': function (evt, template) {
        var nPerPage = Session.get("nPerPage");
        console.log("more");
        Session.set("nPerPage", nPerPage+moreIncrement);
    }
})

Any ideas? I am open to other solutions -- I tried to figure out how to trigger an event when the "more" button becomes visible...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Chet
  • 18,421
  • 15
  • 69
  • 113

2 Answers2

1

On event handler try

  e.stopPropagation()
  e.preventDefault()

return false from event handler

Jim Mack
  • 1,437
  • 11
  • 16
  • There is probably a right way to solve this. If in a deadline, and you had to fix this ASAP, I would set a timestamp a second in the future, then only allow a new event if we were past that buffer. Can you set a debug on the triggered event for the new insert, and look at its callstack for clues? – Jim Mack Sep 04 '13 at 08:36
  • both -- I could probably figure out the timestamp though – Chet Sep 04 '13 at 19:59
  • add to the top of your js in the second handler that shouldn't be called: debugger; Some browsers will stop when triggered. Others, sometimes you have to have the debug window open (FF for me). Commonly, right click on page and choose inspect element. Look per your favorite browser. – Jim Mack Sep 04 '13 at 20:25
  • timestamp: ignoreUntil = new Date(new Date().getTime + 1000); if (new Date() > ignoreUntil) then { } In the event above, add Meteor.ignoreUntil = new Date(new Date().getTime + 1000)); Then in the event that gets triggered if (new Date() > ignoreUntil) then { – Jim Mack Sep 04 '13 at 20:25
  • sounds good. is there a way to capture an arbitrary touch event? to buttons, hyperlinks, etc. – Chet Sep 04 '13 at 20:36
  • If you have the source, put debug there. To start from an element and investigate is harder, as many libraries do things many different ways. This is a good place to start: http://stackoverflow.com/questions/1527215/unobtrusive-javascript-obfuscates-event-handling – Jim Mack Sep 04 '13 at 21:58
0

Jim's advice helped me, but instead e - use event:

event.stopPropagation()
event.preventDefault()
FazoM
  • 4,777
  • 6
  • 43
  • 61