我正在尝试为https://github.com/RubaXa/Sortable编写一个附加的 aurelia 行为,该行为允许使用 html 5 拖放重新排序列表中的项目,类似于它们为 AngularJS 设置的。到目前为止,html 元素的拖放与以下模板代码完美配合
<template>
<require from="./sortable"></require>
<section>
<h2>${title}</h2>
<ul sortable>
<li repeat.for="item of items">${item.title}</li>
</ul>
</section>
</template>
和附加行为
import {Behavior} from 'aurelia-framework';
import Sortable from 'rubaxa-sortable';
export class SortableAttachedBehavior {
static metadata() {
return Behavior
.attachedBehavior('sortable');
}
static inject() {
return [Element];
}
constructor(element) {
this.element = element;
}
bind(viewModel) {
this.sortable = Sortable.create(this.element);
}
unbind() {
this.sortable.destroy();
this.sortable = null;
}
}
作为下一步,除了重新排序 html 元素之外,我还想重新排序模型中的数据项。我可以从我拥有的可排序实例中获取有关 dnd 所需的事件。我现在的问题是获取对这些项目的引用。
因为我只想在子元素上存在 repeat.for 行为时这样做,所以我认为最好的方法是访问其 Repeat 实例并更新其 items 属性的内容。但是如何检查这种附加行为的存在以及如何获取重复实例?
还是有更好的方法来访问这些项目(除了再次将它们指定为我的可排序行为的属性)?
干杯,蒂尔曼