我应该如何获取此输入字段的值? [英] How should I get the value of this input field?

查看:96
本文介绍了我应该如何获取此输入字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 {{input value =(cents-to-dollars model.amountInCents)}} 中使用子表达式。它正在使用自定义帮助器将值从美分转换为美元。我的API返回美分。

I am using a subexpression at {{input value=(cents-to-dollars model.amountInCents)}}. It is using a custom helper to convert the value from cents to dollars. My API returns cents.

但是在控制器中 save action, console.log this.get('model.amountInCents')); 返回 undefined 。我错过了什么吗?可能在输入帮助器中 name valueBinding

However in the controllers save action, console.log(this.get('model.amountInCents')); returns undefined. Am I missing something? Maybe name or valueBinding in the input helper?

如果我删除子表达式。 console.log(this.get('model.amountInCents')); 输出正常。

If I remove the subexpression. console.log(this.get('model.amountInCents')); outputs fine.

// Routes
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('product', params.product_id);
  }
});

// Controller
export default Ember.Controller.extend({
  actions: {
    save: function() {
      console.log(this.get('model.amountInCents')); // returns undefined
      var _this         = this;
      var dollars       = this.get('model.amountInCents');
      var amountInCents = dollars / 100;

      this.get('model').set('amountInCents', amountInCents);

      this.get('model').save().then(function(product){
        _this.transitionToRoute('admin.products.show', product);
      }, function() {
        // Need this promise, so we can render errors, if any, in the form
      });

      return false;
    },
    cancel: function() {
      this.transitionToRoute('products.show', this.get('model'));
    }
  }
});

// Template
<form {{action "save" on="submit"}}>
  <p>
    <label>Name:
      {{input value=model.name}}
    </label>
  </p>

  <p>
    <label>Amount in cents:
      {{input value=(cents-to-dollars model.amountInCents)}}
    </label>
  </p>

  <input type="submit" value="Save"/>
  <button {{action "cancel"}}>Cancel</button>
</form>


推荐答案

首先,(至少在版本1.9中) 1)您提出的建议并不奏效(请参阅此处 - 该值显示在输入字段外部)。我认为真正的问题是,你没有绑定到一个属性,而是绑定到一个帮助器返回的字符串(这不是你想要的)。

First of all, (at least in version 1.9.1) what you are proposing doesn't really work (see here - the value appears outside of the input field). The real problem, I think, is that you are not binding to a property and instead are binding to a string returned from a helper (which is not what you want).

所以,你能做什么?

你可以设置一个 $ 计算属性如下:

You can set up a dollars computed property as follows:

App.IndexController = Ember.ObjectController.extend({
  dollars: function(key, value){
    if (arguments.length > 1) {
      var dollars = value;
      this.set('amountInCents', parseInt(dollars) * 100);
    }

    return this.get('amountInCents') / 100;
  }.property('model.amountInCents')
});

全工作示例 here

这篇关于我应该如何获取此输入字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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