简体   繁体   English

Aurelia-dialog使用Attach-重点放在自定义元素上

[英]Aurelia-dialog using attach-focus on custom element

I'm trying to pass attach-focus="true" to one of the inner elements of a custom element so that the correct element will receive the focus when aurelia-dialog opens. 我试图将attach-focus="true"传递给自定义元素的内部元素之一,以便当aurelia-dialog打开时正确的元素将获得焦点。

Custom element: enum-list.html 自定义元素:enum-list.html

<template>
  <label class="control-label">${label} DEBUG: ${attach-focus}</label>
  <select class="form-control" value.bind="value" attach-focus.bind="attach-focus">
    <option if.bind="data" repeat.for="code of data | keys" value="${code}">${data[code]}</option>
  </select>
</template>

Custom element: enum-list.js 自定义元素:enum-list.js

import { bindable, bindingMode } from 'aurelia-framework';
export class EnumListCustomElement {
  @bindable label;
  @bindable data;
  @bindable attach-focus; // <-- Maybe the source of the error?
  @bindable({ defaultBindingMode: bindingMode.twoWay }) value;
}

Dialog template: edit-locale.html: 对话框模板:edit-locale.html:

<template>
  <ai-dialog>
    <ai-dialog-header class="modal-header modal-header-success">
      <h4 class="modal-title">Edit Locale</h4>
    </ai-dialog-header>
    <ai-dialog-body>
      <form>
        <enum-list attach-focus="true" label="Language" data.bind="core.enums.SystemLanguage" value.bind="sch_lang"></enum-list>
        <enum-list label="Currency" data.bind="core.enums.CurrencyCode" value.bind="sch_currency"></enum-list>
      </form>
    </ai-dialog-body>
    <ai-dialog-footer>
      <button type="button" click.trigger="dialogController.cancel()">Cancel</button>
      <button type="button" click.delegate="dialogController.ok()">Save</button>
    </ai-dialog-footer>
  </ai-dialog>
</template>

Instantiation (from my VM js): 实例化(来自我的VM js):

this.dialogService.open({ viewModel: EditLocale, model: this.record, lock: true }).then(response => {

The modal dialog loads fine if I remove the dashes from attach-focus in edit-locale.js and inside the custom element. 如果我从edit-locale.js和自定义元素内的attach-focus中删除破折号,则模式对话框将很好地加载。 But with the dash, I'm getting an error: Uncaught SyntaxError: Unexpected token import . 但随着破折号,我得到一个错误: Uncaught SyntaxError: Unexpected token import I think the dash is interfering but I don't know how to fix it. 我认为破折号正在干扰我,但我不知道如何解决。

My preference is to fix it so that the instantiation of the custom control has the standard parameter attach-focus="true" (with the dash) so that it's consistent with normal elements like INPUT and SELECT. 我更喜欢对其进行修复,以使自定义控件的实例化具有标准参数attach-focus="true" (带有破折号),从而使其与INPUT和SELECT之类的常规元素保持一致。

You are right about the source of the error, you can't have a property-name containing a dash. 您对错误的源是正确的,不能有包含破折号的property-name Because it reads as property - name . 因为它读为property - name

There is a convention in aurelia ( link to docs , search for dash-case) to map attributes and elements name from dash notation to camelCase notation, so if in your model you will name your bindable property as @bindable attachFocus - you will be able to use it in you views as attach-focus.bind="true". aurelia中有一个约定( 链接到docs ,搜索破折号)以将属性和元素名称从破折号表示法映射到camelCase表示法,因此,如果在模型中将可绑定属性命名为@bindable attachFocus您将能够以在您的视图中将其用作attach-focus.bind =“ true”。

Also make sure that you <require> your custom elements/attributes in your views or make them globally available when configuring aurelia. 另外,在配置aurelia时,请确保您<require>视图中的自定义元素/属性或使它们在全局范围内可用。

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

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