【问题标题】:MVC controller changes input field after submissionMVC 控制器在提交后更改输入字段
【发布时间】:2015-04-26 07:19:20
【问题描述】:

我的表单中有一个金额字段,我在控制器中提交后乘以 100。 问题是视图在进入下一页之前渲染了乘法。 我怎样才能避免这种情况? 更一般地,如何防止视图在提交后显示修改后的值?

我在后端使用带有 rails 的 ember.js,但我认为这更像是一个 MVC 问题。

这是视图:

{{input value=model.amount mask="999[999].99"
        classNames="form-control" placeholder=(t 'transactions.amount')}}

这是控制器:

  actions: {
create: function() {
  var _this = this;
  var model = this.get('model');

  model.set('amount', model.get('amount') * 100);

  model.save().then(function(transaction) {
    _this.get('target').transitionToRoute(_this.get('destination'), transaction.get('id'));
  }, function(response){
    _this.set('errors', response.errors);
  });
}

}

【问题讨论】:

  • 请提供您的代码

标签: model-view-controller ember.js


【解决方案1】:

这是使用计算属性的好例子,您将在模板中使用它:

displayAmount: function(key, value, previousValue) {
    // setter
    if (arguments.length > 1) {
      this.set('amount', value * 100);
    }

    // getter
    return this.get('amount') / 100;
  }.property('amount')

计算属性的语法即将更改,因此您可能需要此版本:

displayAmount: Ember.computed("amount", {
  get: function() {
    return this.get("amount") / 100;
  },
  set: function(key, amount) {
   this.set("amount", amount * 100);
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-11
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多