Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove event listener with the new library

Tags:

dart

How do i remove a listener for an event I've previously registered to with the new dart library?

I'm using this to listen for events with the new library:

element.onMouseMove.listen

What is the equivalent version of this?

element.on.mouseMove.remove
like image 627
Ali Akbar Avatar asked Jan 23 '13 09:01

Ali Akbar


People also ask

How do I get rid of added event listener?

JavaScript | removeEventListener() method with examples The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.

Should I remove event listeners before removing elements?

If there is no memory leak, the used memory will increase by around 1000kb or less after the tests are run. However, if there is a memory leak, the memory will increase by about 16,000kb. Removing the event listener first always results in lower memory usage (no leaks).

When should you remove event listener?

Since we only need the listener for our modal, it should be removed whenever the user cannot interact with our modal any longer. The same is true for any element that can be toggled as well as animations on elements.

Do event listeners get removed?

According to the jquery Documentation when using remove() method over an element, all event listeners are removed from memory. This affects the element it selft and all child nodes. If you want to keep the event listners in memory you should use . detach() instead.


2 Answers

element.onMouseMove.listen(...) returns a StreamSubscription. Simply call its cancel() method to stop receiving events.

like image 147
Alexandre Ardhuin Avatar answered Nov 06 '22 00:11

Alexandre Ardhuin


In addition to Alexandre's answer, a concrete example in code would look like this:

import 'dart:html';

void main() {
  var listener = (e) => print("Moved");

  var element = query("#text");
  var streamsub = element.onMouseMove.listen(listener); // <--- add the listener

  var reverseText= (Event event) {
    var text = query("#text").text;
    var buffer = new StringBuffer();
    for (int i = text.length - 1; i >= 0; i--) {
      buffer.add(text[i]);
    }
    query("#text").text = buffer.toString();

    streamsub.cancel(); // <----   stop listening
  };

  element
    ..text = "Click me!"
    ..onClick.listen(reverseText);    
}

This prints "Moved" when you move the mouse over the "Click Me!" text (from the standard dart sample). When clicked, it stops.

like image 20
Chris Buckett Avatar answered Nov 06 '22 01:11

Chris Buckett