简体   繁体   English

Aurelia如何在自定义元素和其自己的命名空间中添加绑定行为?

[英]Aurelia how to add Binding Behavior in a Custom Element and within its own namespace?

I am building an Aurelia Custom Element and I want to use a special Binding Behavior, however I can't seem to be able to use it (declare it) outside of my Custom Element class. 我正在构建一个Aurelia自定义元素,我想使用一个特殊的绑定行为,但我似乎无法在我的自定义元素类之外使用它(声明它)。 Also, I was wondering if namespace exist in Aurelia for Binding Behaviors and other custom stuff? 另外,我想知道Aurelia中是否存在绑定行为和其他自定义内容的命名空间?

Here's the Binding Behavior that I want to use 这是我想要使用的绑定行为

optional-binding.js - code provided by another stack question optional-binding.js - 由另一个堆栈问题提供的代码

export class OptionalBindingBehavior {
  bind(binding, scope, interceptor) {
    binding.originalupdateTarget = binding.updateTarget;
    binding.originalTargetProperty = binding.targetProperty;
    binding.updateTarget = val => {
      if (val === undefined || val === null || val === '') {
        binding.targetProperty = null;
      } else {
        binding.targetProperty = binding.originalTargetProperty;
      }
      binding.originalupdateTarget(val);
    };
  }

  unbind(binding, scope) {
    binding.updateTarget = binding.originalupdateTarget;
    binding.originalupdateTarget = null;
    binding.targetProperty = binding.originalTargetProperty;
    binding.originalTargetProperty = null;
  }
}

I tried import this way 我试着用这种方式导入

index.js index.js

  import {MyCustomElement} from './my-element';
  import './optional-binding.js';

  export function configure(aurelia) {
    aurelia.globalResources('./my-element');
  }

  export {
    MyCustomElement
  };

and also tried to import './optional-binding.js'; 并尝试import './optional-binding.js'; directly inside the Custom Element but in all cases, it keep saying that it can't find this binding. 直接在自定义元素内,但在所有情况下,它一直说它找不到这个绑定。

I would also like to know how to make this into it's own namespace (if possible) to avoid naming collision with custom binding behaviors that might be declared globally by other code/project. 我还想知道如何将它放入它自己的命名空间(如果可能的话),以避免命名与可能由其他代码/项目全局声明的自定义绑定行为的冲突。

EDIT 编辑

I tried the suggestion of @janmvtrinidad to import the binding behavior with a require but I get similar result as I had before when trying to import it in the ViewModel. 我尝试使用@janmvtrinidad的建议来导入带有require的绑定行为,但是我在尝试在ViewModel中导入它时获得了类似的结果。 Also note that all my files are within the same directory, so using ./ should be sufficient. 另请注意,我的所有文件都在同一目录中,因此使用./应该足够了。 Up until now, I'm stuck in declaring the binding behavior inside the same file as the custom element, that is the only way it works so far. 到目前为止,我一直在声明与自定义元素相同的文件中的绑定行为,这是它到目前为止唯一的工作方式。

inside the Custom Element View (template) 自定义元素视图(模板)内

<template>
    <require from="./optional-binding"></require>
    ...

I get the error that it can't find it 我得到了它无法找到的错误

Cannot find module './aurelia-bootstrap-select/optional-binding'

Entire Code/Project 整个守则/项目

I also released this plugin to the community some time ago, if you want to see the entire code, the plugin is available as Aurelia-Bootstrap-Select . 我也在不久前向社区发布了这个插件,如果你想查看整个代码,插件可以作为Aurelia-Bootstrap-Select使用 Currently if someone wants to use it and also has an OptionalBindingBehavior then name collision will happen, which is a bummer. 目前,如果有人想要使用它并且还有一个OptionalBindingBehavior则会发生名称冲突,这是一个无赖。

NOTE 注意

Even after I posted a bounty, I am still waiting for the correct way of dealing with namespace to avoid name collision in Aurelia. 即使我发布了赏金之后,我仍在等待处理命名空间的正确方法,以避免Aurelia中的名称冲突。

Assuming that your code structure is generated by Aurelia CLI where elements, binding-behaviors, etc. is in resources folder. 假设您的代码结构由Aurelia CLI生成,其中元素,绑定行为等位于resources文件夹中。 Now you can add your binding-behaviors and custom element as a globalResources . 现在,您可以将绑定行为和自定义元素添加为globalResources In your resources/index.ts , add your binding-behavior path inside config.globalResources([...]) . 在您的resources/index.ts ,在config.globalResources([...])添加绑定行为路径。

export function configure(config: FrameworkConfiguration) {
    config.globalResources([
        './value-converters/json',
        './binding-behaviors/intercept-one',
        './elements/pagination'
    ]);
}

Now you can use your resources globally without require in your html. 现在,您可以在html中全局使用资源而require使用。

In terms of namespaces, Aurelia will throw an error when you try to register globally two resources with the same ClassName. 在命名空间方面,当您尝试使用相同的ClassName注册全局两个资源时,Aurelia会抛出错误。 You have two options when you run in this situation. 在这种情况下运行时有两种选择。

  • You can use @CustomElement and @CustomAttribute decorator to override the existing convention. 您可以使用@CustomElement@CustomAttribute装饰器来覆盖现有约定。 Default convention will assume your ClassName will be converted from InitCaps to dash-case . 默认约定将假定您的ClassName将从InitCaps转换为dash-case More information here . 更多信息在这里
  • Or you can explicitly require it in your html. 或者你可以在你的HTML中明确require它。 It will override the globalResources then use your custom resources inside your html even the globalResource has the same ClassName with your custom binding-behavior/resource. 它将覆盖globalResources然后在html中使用您的自定义资源,即使globalResource与您的自定义绑定行为/资源具有相同的ClassName。

EDIT 编辑

I would also like to know how to make this into it's own namespace (if possible) to avoid naming collision with custom binding behaviors that might be declared globally by other code/project. 我还想知道如何将它放入它自己的命名空间(如果可能的话),以避免命名与可能由其他代码/项目全局声明的自定义绑定行为的冲突。

Currently it is not possible. 目前这是不可能的。 Aurelia viewResources is a singleton. Aurelia viewResources是一个单身人士。 As I believe, it is sufficient for you to add it on your html using require as stated in the 2nd bullet above. 我相信,您可以使用上面第二个子弹中所述的require将其添加到您的html上。

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

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