Jump to content


Photo

w3_removeEvent doesn't work


  • Please log in to reply
3 replies to this topic

#1 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 25 February 2016 - 01:35 PM

Hello,

 

I want to implement a click once event, like in jQuery. But it doesn't work. The jquery alternative does work. Not sure what's wrong with my code:

constructor TMyPopupMenu.Create(AOwner: TW3Component);
begin
  inherited Create(AOwner);

  w3_AddEvent(BrowserAPI.Document, 'click', @DoHide);
  //BrowserAPI.Document.addEventListener('click', @DoHide);

  //=>This works fine
  //jQueryEx(BrowserAPI.Document).one('click', lambda writeln('doc click') end);

end;

procedure TMyPopupMenu.DoHide;
begin
  writeln('doc click');
  //BrowserAPI.Document.removeEventListener('click', @DoHide);
  w3_RemoveEvent(BrowserAPI.Document, 'click', @DoHide);
end;

Does anybody know what I am doing wrong?

 



#2 Christian-W. Budde

Christian-W. Budde
  • Members
  • 334 posts
  • LocationGermany

Posted 25 February 2016 - 08:44 PM

Personally I don't use the SmartCL framework, but I encountered a similar issue when using the pure W3C APIs. The problem is that in the underlying DWScript regular procedures and methods are the same. This makes it easy to add them as event handlers as they are internally wrapped. However, if you remove the event handler, you need to remove the exact same handler that was provided when adding. Unfortunately, it is wrapped again in another handler and thus removal fails.

As said, I'm not sure if this also applies for the w3_RemoveEvent function. To see if this is the case you have to step through adding and removing the event handler on the JS side (to see what is added and what is removed).

A solution for this is to add (and remove) a simple procedure (i.e. not a method!). In this simple procedure you have to write your wrapper code manually. Not beautiful, but it works.

#3 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 26 February 2016 - 11:54 AM

Thanks, for explaining.

Adding and removing a simple procedure does work, but I need to access some data of my class instance. So that deosn't help.

Actually, I want to create a popup menu. I need the single click event in order to hide the popop window, when to user clicks somewhere in the document.



#4 Dany

Dany
  • Members
  • 127 posts

Posted 03 March 2016 - 10:02 PM

Yes, i have seen the same thing. I posted an extensive report here back when but it might be one of those that disappeared. It's a bit confusing to se how the headers overload for a JObject and a TObject based solution. None of them works 100%. Anyway; on the creative side:

 

If you need to add and remove handlers, with the current codegen, the best alternative (IMHO) is t use delegates. In your "constructor" or "initialize" assign a variable the procedure and use the variable to add and remove event handlers. It works, but as CW say, not beautiful.

 

/D






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users