That’s pretty much what I did with dragula, made a custom attribute to stash data/callbacks, I won’t say it was the right way to go about it but it works.
Should be noted that in dragula it sometimes destroys and re-creates elements so you’ll want to handle that. I’ve configured my dragula instance to copy: true
and revertOnSpill: true
instance.on('cloned', (clone, original) => {
clone.stash = original.stash;
});
And here is the custom attribute to stash data on an element
import { inject, customAttribute, bindingMode, dynamicOptions } from 'aurelia-framework';
import { DOM } from 'aurelia-pal';
@customAttribute('stash', bindingMode.twoWay)
@inject(DOM.Element)
@dynamicOptions
export class Stash {
constructor(element) {
this.element = element;
this.element.stash = {};
}
propertyChanged(name, value) {
if (this.element) {
if (!this.element.stash)
this.element.stash = {};
this.element.stash[name] = value;
}
}
}
Usage like
For the drag source
[...]
<tbody class="${allowDrag ? 'drag-source':''}" stash="list.bind: rows;">
<tr repeat.for="row of rows" class="${allowDrag ? 'grabbable':''} gu-border ${row.active ? 'active':''} ${row.class}" css="${row.style}"
stash="item.bind: row; start.bind: start; end.bind: end;" click.delegate="expansion($event, row)">
[...]
For the drag target
[...]
<div class="drop-target" stash="drop.bind: drop; allow.bind: allow; preview.bind: preview;">
[...]
self.drop = (data) => {
// Handle the drop interaction myself
};
self.allow = (data) => {
// Allow the item to drop here
};
self.preview = (data) => {
// Show Drop Overlay that indicates you can drop the thing here
};
In this way stash lets me access the row and callbacks in the dragula events and override the drop event I actually just cancel the drop event and do my own data changes which updates the html.
instance.on('drop', (el, target, source, sibling) => {
safecall(target, 'stash.drop',
{
el: el,
target: target,
source: source,
sibling: sibling
}
);
isCancelled = true;
instance.cancel(); // Prevent html move
isCancelled = false;
});
// You'll also want to handle the cancel interaction
instance.on('cancel', (el, target, source) => {
if (isCancelled)
return;
safecall(source, 'stash.cancel', {
el: el,
target: target,
source: source
});
});
Hopefully some of that is useful to you and not just random junk