[英]Aurelia trying to load HTML from Select2?
So I'm trying to use Select2 within my Aurelia application. 所以我试图在我的Aurelia应用程序中使用Select2。 I installed Select2 using
jspm install select2
, and within my app.html
file I require Select2 using <require from="select2/js/select2.min.js"></require>
. 我使用
jspm install select2
安装了Select2,在我的app.html
文件中,我需要使用<require from="select2/js/select2.min.js"></require>
Select2。 The browser loads the minified JS file fine, but for some reason it also tries to load 浏览器加载缩小的JS文件,但由于某种原因它也尝试加载
http://localhost:3003/jspm_packages/github/select2/select2@4.0.0/js/select2.min .html . http:// localhost:3003/jspm_packages/github/select2/select2@4.0.0/js/select2.min .html 。
Why is Aurelia trying to load the HTML counterpart of the same JS file that I specified in my <require>
element? 为什么Aurelia尝试加载我在
<require>
元素中指定的同一JS文件的HTML副本? How can I fix this? 我怎样才能解决这个问题?
Thanks 谢谢
The purpose of <require from="...."></require>
is to import a view resource into your view. <require from="...."></require>
是将视图资源导入视图。 View resources are things like custom elements or custom attributes. 查看资源是自定义元素或自定义属性。 When you add
<require from="select2/js/select2.min.js"></require>
to your template aurelia loads the module and thinks it's the view-model for a custom element. 当您将
<require from="select2/js/select2.min.js"></require>
到模板时,aurelia会加载模块并认为它是自定义元素的视图模型。 It then attempts to load the view for it which is why you see the attempt to load .../select2.min.html
然后它会尝试为它加载视图,这就是为什么你看到加载
.../select2.min.html
的尝试
The "aurelia way" to integrate select2 would be to create a custom attribute that applies select2 to the element. 集成select2的“aurelia方式”是创建一个将select2应用于元素的自定义属性。 Something like this:
像这样的东西:
select2-custom-attribute.js 选择2 -定制attribute.js
import {customAttribute, inject} from 'aurelia-framework';
import {DOM} from 'aurelia-pal';
import $ from 'jquery';
import 'select2'; // install the select2 jquery plugin
import 'select2/css/select2.min.css!' // ensure the select2 stylesheet has been loaded
@customAttribute('select2')
@inject(Element)
export class Select2CustomAttribute {
constructor(element) {
this.element = element;
}
attached() {
$(this.element)
.select2(this.value);
//.on('change', () => this.element.dispatchEvent(DOM.createCustomEvent('change')));
}
detached() {
$(this.element).select2('destroy');
}
}
Then you'd import the custom attribute into your view and use it like this: 然后,您将自定义属性导入到视图中,并像这样使用它:
app.html app.html
<require from="select2-custom-attribute"></require>
<select select2 value.bind="selectedState">
<option repeat.for="state of states" model.bind="state">${state.name}</option>
</select>
Or like this if you need to pass some options to select2 (this assumes your view-model has a property named "options" containing the select2 options as described in their docs): 或者像这样,如果你需要将一些选项传递给select2(这假设你的视图模型有一个名为“options”的属性,其中包含其文档中描述的select2选项):
app.html app.html
<require from="select2-custom-attribute"></require>
<select select2.bind="options" value.bind="selectedState">
<option repeat.for="state of states" model.bind="state">${state.name}</option>
</select>
Here's a working example: https://gist.run/?id=0137059e029fc4b3ccd367e385f47b19 这是一个有效的例子: https : //gist.run/?id = 0137059e029fc4b3ccd367e385f47b19
Unfortunately I was unable to import select2 properly using jspm, even when using the shim listed here . 不幸的是,我无法使用jspm正确导入select2,即使使用此处列出的垫片也是如此。 If you hit the same issue, you'll have to remove the import statements related to select2 from the custom attribute code above and load the select2 js and css with script/link tags in your document.
如果遇到同样的问题,则必须从上面的自定义属性代码中删除与select2相关的import语句,并在文档中加载带有脚本/链接标记的select2 js和css。
For the sake of completion, firing the change
event in Jeremy's solution results in a recursive loop and causes exception to fix it I had to do: 为了完成,在Jeremy的解决方案中触发
change
事件会导致递归循环并导致异常以修复它我必须执行的操作:
attached() {
$(this.element)
.select2(this.value)
.on('change', evt => {
if (evt.originalEvent) { return; }
this.element.dispatchEvent(new Event('change'));
});
}
I could then do: 然后我可以这样做:
<require from="select2-custom-attribute"></require>
<select select2.bind="options" value.bind="selectedState" change.delegate="changeCallback($event)">
<option repeat.for="state of states" model.bind="state">${state.name}</option>
</select>
changeCallback($event)
can be a function on your vm
eg changeCallback($event)
可以是你的vm
上的一个函数,例如
changeCallback(evt: Event): void {
console.log(evt);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.