Jump to content

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?

 

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.

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

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...