绑定selectize与奥里利亚

问题描述:

我想使用selectize与aurelia.io 我已经创建的模板shop.html绑定selectize与奥里利亚

<template> 
    <input ref="content" type="text" value.two-way="shops" > 
</template 
类店

与adnotation @customElement( '店')我 我想要绑定selectize.js@Bindable商店

attached() { 
    var s = $(this.content).selectize({ 
     delimiter: ',', 
     persist: false,  
     create: function(input) { 
      return { 
       value: input, 
       text: input 
      } 
     }   
    });} 

我使用这个自定义元素与模板的书,像这样:

<shop shops.two-way="selected.data.bookshops" ></shop> 

双向数据绑定不工作如我所料。 选择性值只是第一次更新。

您必须安装selectize取决于microplugin的jQuery

jspm jquery 
jspm install selectize 
jspm install sifter 
jspm install microplugin 

然后你可以导入和使用它

import $ from 'jquery'; 
import selectize from 'selectize'; 
+0

我已经完成了。问题在于绑定选择对象。我想在selectize输入中实现双向数据绑定。 –

我已经找到了需要在我的自定义元素中捕获selectize的更改事件并将其传播到o严格的选择输入,以便Aurelia的数据绑定工作。在选择上添加更改处理程序以传播事件。

// init selectize 
    this.sel = el.selectize(opts)[0]; // first element 

    // on change after setting initial value 
    this.sel.selectize.on('change',() => { 
     // no event param passed in 
     console.log(`Selectize change event`); 
     // dispatch to raw select within the custom element for data binding trigger 
     // bubble it up to custom event in case change event is handled 
     let notice = new Event('change', {bubbles: true}); 
     $(el)[0].dispatchEvent(notice); 
    }); 

通过冒泡的变化情况,这也将让你当它在一个视图中使用您的自定义元素上添加change.delegate处理程序。

<selectize ... change.delegate='changeHandler()'> 
+0

请不要张贴链接到要点,把代码放入你的答案。请参阅[答案] – durron597

+0

这是不正确的。从您的常见问题解答:只要提供上下文,“鼓励与外部资源的链接......”。但是,我编辑了我的答案,因为我的方法稍微改变了原来的要点。 – mujimu

+0

[请阅读这个元问题,因为它直接解决你的问题](http://meta.*.com/questions/326744/github-links-as-answers/326745#326745) – durron597