MVC 控制器在提交后更改输入字段
MVC controller changes input field after submission
我的表单中有一个金额字段,在我的控制器中提交后我将其乘以 100。
问题是视图在移动到下一页之前呈现乘法。
我怎样才能避免这种情况?
更一般地说,如何防止视图在提交后显示修改后的值?
我在后端使用 ember.js 和 rails,但我认为这更像是一个 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);
});
}
}
这将是使用计算的 属性 的好案例,您将在模板中使用它:
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);
}
})
我的表单中有一个金额字段,在我的控制器中提交后我将其乘以 100。 问题是视图在移动到下一页之前呈现乘法。 我怎样才能避免这种情况? 更一般地说,如何防止视图在提交后显示修改后的值?
我在后端使用 ember.js 和 rails,但我认为这更像是一个 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);
});
}
}
这将是使用计算的 属性 的好案例,您将在模板中使用它:
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);
}
})