Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"no obj for key" error #216

Open
xtagon opened this issue Oct 18, 2023 · 0 comments
Open

"no obj for key" error #216

xtagon opened this issue Oct 18, 2023 · 0 comments

Comments

@xtagon
Copy link

xtagon commented Oct 18, 2023

Drag and drop actions throw an error if the payload being dropped is not a draggable object, such as if it is a text node being dragged.

This same error is reproduced in various other ways in these issues:

#157

#101

#21

#62

And previously this was "fixed" but not completely, in:

https://github.com/mharris717/ember-drag-drop/pull/96/files

The error is not catchable in the @action handlers because the error prevents that action from being sent, so the only workaround is to monkey patch the DraggableObjectTarget component.

One workaround is to extend DraggableObjectTarget with:

import DraggableObjectTarget from 'ember-drag-drop/components/draggable-object-target';

function isNoObjForKeyError(error) {
  return error && error.message && error.message.startsWith("no obj for key ");
}

export default DraggableObjectTarget.extend({
  // See https://github.com/mharris717/ember-drag-drop/blob/v0.9.0-beta.0/addon/components/draggable-object-target.js#L22-L25
  handlePayload(payload, event) {
    let obj;

    try {
      obj = this.get('coordinator').getObject(payload,{target: this});
    } catch (error) {
      if (isNoObjForKeyError(error)) {
        // Intentionally suppress this error so it doesn't throw on non-object
        // drops (e.g. if you drag and drop text instead of a draggable object)
      } else {
        throw error;
      }
    }

    if (obj) {
      this.get('action')(obj, { target: this, event: event });
    }
  }
});

An alternative would be to send the action even if obj is null or undefined, for those of you who want to accept non-object drops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant