前端框架Aurelia - Value Converter

本文介绍Aurelia框架中值转换器的功能与使用方法,包括数据流向标识、参数绑定、多参数支持及组合使用等内容,并通过实例展示DateFormatValueConverter和CurrencyFormatValueConverter的应用。

1.Value Converter

Aurelia的value converter的改进:

  1. The Aurelia ValueConverter interface uses toView and fromView methods, which make it quite clear which direction the data is flowing. This is in contrast to Xaml's IValueConverter, which uses Convert and ConvertBack.
Aurelia的value converter用toView和fromView方法来标识数据流方向。
2.In Aurelia, converter parameters can be data-bound. This is something that was missing in Xaml and enables more advanced binding scenarios.

转换的参数可以被数据绑定。

3.Aurelia value converter methods can accept multiple parameters.

Aurelia的值转换方法可以接受多参数。

4.Multiple value converters can be composed using pipes (|).

多个值转换可以用 | 。


2.Simple Converters

http://aurelia.io/hub.html#/doc/article/aurelia/binding/latest/binding-value-converters/3

Well, first we created a couple of value converters: DateFormatValueConverter and CurrencyFormatValueConverter. Each has a toView method that the Aurelia framework will apply to model values before displaying them in the view. Our converters use the MomentJS and NumeralJS libraries to format the data.

toView方法,aurelia框架在view上显示values前会先向ValueConverter申请model values。

Next, we updated the view to require the converters so they can be used in the view. When requiring a resource such as a value converter, you supply the path to the resource in the require element's from attribute.

要引入文件的意思咯。

<require from="./date-format"></require>
<require from="./currency-format"></require>

When Aurelia processes the resource, it examines the class's metadata to determine the resource type (custom element, custom attribute, value converter, etc). Metadata isn't required, and in fact our value converters didn't expose any. Instead, we relied on one of Aurelia's simple conventions: export names ending with ValueConverter are assumed to be value converters. The convention registers the converter using the export name, camel-cased, with the ValueConverter portion stripped from the end.

Aurelia有一个简单的约定,我们用ValueConverter来作为value converter文件名的结尾。

  • DateFormatValueConverter registers as dateFormat
  • CurrencyFormatValueConverter registers as currencyFormat

Finally, we applied the converter in the binding using the pipe | syntax:


下面的代码是给value converter绑定变量,这样就是哪个value converter format 哪个变量。

${currentDate | dateFormat} <br/>
${netWorth | currencyFormat}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值