【发布时间】:2017-09-15 21:33:56
【问题描述】:
我已经创建了 aurelia 自定义属性代码如下
@customAttribute('bt-select')
@inject(Element)
export class BootstrapSelect {
@bindable val;
@bindable list;
constructor(element) {
this.element = element;
}
attached() {
$(this.element).selectpicker();
}
valChanged(newValue, oldValue) {
$(this.element).selectpicker("val",_.isObject(newValue)?newValue.id:newValue);
}
listChanged(newValue, oldValue) {
setTimeout(()=>{
$(this.element).selectpicker("refresh");
},300)
}
detached(){
this.val = null;
this.list = null;
$(this.element).selectpicker("destroy");
}
}
并如下使用它
<select value.bind="form.category" data-width="100"
bt-select="val.bind:form.category;list.bind:categorys;">
<option value="">select:category</option>
<option repeat.for="category of categorys"
value="${category.id}">
${category.name}
</option>
</select>
选择的选项标签从类道具类别重复,this.categorys ajax api 的 loda 数据,这是渲染选择选项标签的异步步骤
我必须在listChanged 方法中添加setTimeout func 以等待aurelia 渲染select 的选项标签完成,然后强制刷新bootstrap-select 组件
我觉得不好,但我没有更好的方法来解决它, 我知道许多 jQuery 插件应该使用完整的 dom 元素并渲染它们,但是在 aurelia 框架 attach() 方法中,一些数据从异步 api 加载 在异步数据绑定到 dom 后是否有一些后处理方法或事件可以调用
【问题讨论】: