1

I'm working on drag and drop div and I have the following code:

 void init() {


    ...
    baseWindowTitleDiv.on.mouseDown.add((MouseEvent event) => saveWindowXY(event));
    baseWindowTitleDiv.on.mouseUp.add((MouseEvent event) => stopMoveEvent(event));
    ...        
  }
  void saveWindowXY(MouseEvent event) {
    ...

    document.on.mouseMove.add((MouseEvent event) => runMoveEvent(event));

  }

  void stopMoveEvent(MouseEvent event) {
    print('stopMoveEvent call');    
    document.on.mouseMove.remove((MouseEvent event) => runMoveEvent(event));//does not work!    
  }

  void runMoveEvent(MouseEvent event) {
    ...
  }

I do not understand how can I remove listener from document.on.mouseMove

Full code is here - https://gist.github.com/OZKA/5795352

4

1 回答 1

2

As far as I know, you'd need to have a handle to the event that you added, and use that to remove it later, for example:

var myEvent = (MouseEvent event) => runMoveEvent(event); // store the function in a variable.

void init() {
  document.on.mouseMove.add(myEvent); // refer to the function by name
}


void stopMoveEvent(MouseEvent event) {
  document.on.mouseMove.remove(myEvent); // remove the same function.
}

void runMoveEvent(event) { ... }

What you're doing in your code is adding one anonymous function, and removing a different anonymous function. I think that http://dartbug.com/144 (tagged WontFix) probably provides more details about why this doesn't work.

于 2013-06-17T10:12:06.000 回答