简体   繁体   English

Aurelia和Select2更改活动

[英]Aurelia and Select2 change event

How can I utilize Aurelia's data binding with Select2? 如何利用Aurelia与Select2的数据绑定? It seems to work just fine with a standard select. 它似乎与标准选择一样好用。

Ideally I would like to use change.delegate in the select element to call a method within my View Model so I would have access to the data storage we are injecting. 理想情况下,我想在select元素中使用change.delegate来调用View Model中的方法,这样我就可以访问我们正在注入的数据存储。

The only way I can get an event to fire is to wire up a change event in the attached() handler, but then the data storage falls out of scope. 我可以触发事件的唯一方法是连接附加()处理程序中的更改事件,但随后数据存储超出范围。

View: 视图:

<template>
    <label>Company:</label>
    <!-- i would like to use change.delegate="change()" below -->
    <select value.bind="selectedCompanies" multiple>  
        <option repeat.for="company of companies" model.bind="company.CompanyId">${company.CompanyName}</option>
    </select>
</template>

View Model: 查看型号:

import {inject, bindable} from 'aurelia-framework'; 
import {FilterCompanyData} from 'data/custom elements/filters/filterCompanyData';
import {UserStorage} from 'storage/userStorage';

@inject(Element, FilterCompanyData, UserStorage)
export class FilterCompanyCustomElement {
    @bindable selectedCompanies;

    constructor(element, filterCompanyData, userStorage) {
        this.element = element;
        this.filterCompanyData = filterCompanyData;
        this.userStorage = userStorage;
        this.companies = [];
    }

    bind(bindingContext, overrideContext) {
        let userId = this.userStorage.userId;

        return this.filterCompanyData
            .getCompanies(userId)
            .then(response => this.companies = response);
    }

    attached() {
        var el = $(this.element).find('select');
        var sel = el.select2({ 
            closeOnSelect: false
        });

        // Is it possible to get rid of this? 
        sel.on('change', function (event) {
            if (event.originalEvent) { return; }

            // TODO add changed data to user storage

            var IE = ((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true));
            var notice = IE ? new CustomEvent('change', { bubble: false }) : new Event('change', { bubble: false });

            $(el)[0].dispatchEvent(notice);
        });
    }

    detached() {
        $(this.element).find('select').select2('destroy');
    }

    change() {
        // I'd like to use this method from change.delegate
        // TODO add changed data to user storage
    }
}

On a side note doesn't Aurelia have a built in polyfill for New Event? 另外,Aurelia没有内置的Polyfill用于New Event? IE doesn't seem to like that. IE似乎不喜欢这样。

I see that this question is getting old, but i have tried to do the same that you are doing here, and i ended up with the following: 我看到这个问题已经老了,但是我试图做你在这里做的事情,我最终得到了以下内容:

https://github.com/Kla3mus/select24aurelia https://github.com/Kla3mus/select24aurelia

I don't use the polyfill, or the original events outside of the component. 我不使用polyfill,也不使用组件外部的原始事件。 I use two-way binding for the option and selected value instead. 我对选项和选定值使用双向绑定。 Maybe that could work for you too? 也许那对你也有用吗?

It's written in TypeScript, but it's almost the same as JavaScript, so i think you will be able modify it :) 它是用TypeScript编写的,但它与JavaScript几乎相同,所以我认为你可以修改它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM