Hi, I have implemented a controller which instantiates drag and drop functionality using the Sortable library, and I want to know how I can keep the controller generic whilst defining what should happen in the onEnd event for a specific use case.
Here is the code:
import { Controller } from "stimulus";
import Sortable from "sortablejs";
export default class extends Controller {
connect() {
this.sortable = Sortable.create(this.element, {
onEnd: this.onEnd
})
}
onEnd() {
// This code will depend on the context, how should it be arranged?
}
}
Well the piece I’m currently working on is around allowing a user to specify the order in which something appears. So there is a position attribute on the model and collections are ordered by it.
So in this scenario the user is dragging collection items into their specified order. Once they release the draggable element, I want to iterate over the hidden position fields that each of them has and set them to 1, 2, 3, 4 etc. The code is something like:
There’s a lot going on there that I don’t understand, so will have to take the time to properly absorb it when I get the chance. Thanks for taking the time to reply.