2015-04-26 36 views
0

我的表單中有一個數量字段,在我的控制器中提交後,我乘以100。 問題在於,在轉到下一頁之前,視圖呈現乘法運算。 我該如何避免這種情況? 更一般而言,如何防止視圖在提交後顯示修改後的值?MVC控制器在提交後更改輸入字段

我在後端使用了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); 
    }); 
} 

}

+1

您能否提供您的代碼 –

回答

0

這是使用一個計算的屬性,你可以使用一個很好的例子在您的模板中:

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); 
    } 
})