Aurelia,如何进行可选装订 [英] Aurelia, how to make optional binding

查看:56
本文介绍了Aurelia,如何进行可选装订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Aurelia是否支持可选绑定?我在任何地方都找不到此信息.我遇到的问题是我有一个title属性,该属性可能会或可能不会填充到对象数组中.我使用repeat.fortitle.bind,但是如果此属性不是对象数组的一部分,则我根本不希望该属性存在. Aurelia有可能吗?

Does Aurelia support optional binding? I can't find this information anywhere. The issue that I'm having is that I have a title attribute that might or might not be populated in an array of objects. I use a repeat.for and title.bind but I don't want the attribute to exist at all if this property is not part of the array of objects. Is that possible in Aurelia?

Bootstrap-Select与空的title一起使用时,会引发错误.即时创建Aurelia属性可以解决我的问题.

When using Bootstrap-Select with an empty title it throws an error. Having Aurelia to create attribute on the fly would resolve my issue.

到目前为止,我的代码看起来像这样

The code that I have so far, looks like this

<select class="selectpicker" value.bind="value" options.bind="options" disabled.bind="disabled">
<option repeat.for="option of options" model.bind="option"
        data-subtext.bind="option.subtext"
        title.bind="option.title">
        ${option.name}
</option>

在此示例中,我想将data-subtexttitle作为可选属性.可以吗?

In this example, I would like to make data-subtext and title as optional attributes. Is that doable?

由于这是一个自定义元素,因此我尝试删除对象delete this.element.titletitle属性,但这似乎不起作用.我也尝试过使用jQuery,但是还是没有运气.

Since this is a Custom Element, I tried deleting the title property of my object delete this.element.title but that doesn't seem to work. I also tried with jquery but again no luck.

推荐答案

在很多情况下,我没有对此进行测试,但是我认为您也可以创建自定义绑定行为,例如:

I did not test this in many cases, but I think you could also create a custom binding behavior, like this:

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;
  }
}

然后像这样使用它:

<a href.bind="message & optional">${message}</a>

这篇关于Aurelia,如何进行可选装订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆