Jump to content
markus_ja

w3_removeEvent doesn't work

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×