绑定计算属性的文本字段灰烬 [英] Bindings Computed Property in Ember TextField

查看:138
本文介绍了绑定计算属性的文本字段灰烬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的数据模型绑定到Ember.js文本字段。该模型具有重新presents货币值的字段(例如,$ 1,000.50)。然后,用户可以更改此值。

I'm trying to bind my data model to the text field in Ember.js. The model has a field that represents a currency value (e.g., $1,000.50). The user can then change this value.

灰烬接收数据格式化的号码(1000.50) - 而不是货币。我绑定的视图具有很好的格式的计算性能。这里是我的把手的模板。

Ember receives the data as a Number (1000.50) -not currency formatted. I bind the view to a computed property that has the nice format. Here is my Handlebars template.

{{input classNames="amount" valueBinding="p.amountFmt"}}</td>

我的模型是这样的:

My model looks like:

App.Product = Ember.Object.extend({
  amount : 0.00,
  amountFmt: function () {
    //example. Do actual formatting with this.get('amount'); 
    var formattedNum = '1,000.50'; 
    return formattedNum;
  }.property('amount')
});

的问题是,当用户在输入字段改变量,在我的模型的基本'量'属性不被更新-I假设,因为它结合于所计算的属性

The problem is, when the user changes the amount in the input field, the underlying 'amount' property on my model is not updated -I suppose because it's bound to the computed property.

什么是采取用户的输入,分析并在必要时进行验证,并将其存储在'量'属性的正确方法是什么?我应该使用两个属性之间的绑定/观察员?如何将这项工作?

What is the correct way to take the user's input, parse and validate if necessary, and store it in the 'amount' property? Should I use bindings/observers between the two properties? How would this work?

最终,'量'属性是得到的坚持服务器端。我真的只是考虑计算物业办UI格式的地方。我通常会用一个把手帮手这种格式,但你不能用一个把手帮助的结果相结合文本域的valueBinding。

Ultimately, the 'amount' property is what get's persisted server side. I really just consider the computed property a place to do UI formatting. I would normally use a Handlebars helper for this kind of formatting but you can't combined the TextField's valueBinding with the results of a Handlebars helper.

在此先感谢您的帮助!
安德鲁

Thanks in advance for your help! Andrew

推荐答案

为计算财产 amountFmt 函数作为根据上下文既是一个getter和setter。它的签名是 amountFmt(键,[值])。的值是可选的,并且仅在当该值是要改变通过。

The function for the computed property amountFmt acts as both a getter and setter depending on the context. It's signature is amountFmt(key, [value]). The value is optional and only passed in when the value is to be changed.

amountFmt: function(key, value) {
  if (value) {
    // setter
  } else {
    // getter
  }
}.property('amount')

这篇关于绑定计算属性的文本字段灰烬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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