1
我的目標是計算基準價格和售價之間的保證金百分比。但我也希望能夠在保證金變化時重新計算價格。我不能僅僅通過property()
觀察變化,因爲價格和利潤率將無限重新計算。相反,我想運行keyup
和change
事件的計算,這正是我如何在JQuery中實現相同的任務。 但Ember似乎不支持input
幫手的這種事件。只有keypress
支持,並且我無法使用keypress
實現期望的行爲。計算相關輸入值
模板:
{{input value=basePrice placeholder='Base Price' action="updateMarginPercent" on="key-press"}}
{{input value=price placeholder='Price' action="updateMarginPercent" on="key-press"}}
{{input value=marginPercent placeholder='Margin Percent' action="updatePrice" on="key-press"}}
控制器:
App.ApplicationController = Ember.Controller.extend({
basePrice: 100,
price: 100,
marginPercent: 0,
actions: {
updateMarginPercent: function() {
debugger;
this.set('marginPercent', (this.get('price') - this.get('basePrice')) * 100.0/this.get('basePrice'));
},
updatePrice: function() {
this.set('price', this.get('basePrice') * (this.get('marginPercent') + 100)/100);
}
}
});
JSBin:http://jsbin.com/yevar/1/edit?html,js,output
其實我已經看到你的答案,但沒有理解如何將其應用於我的情況。感謝您用我的jsbin顯示它!我還需要處理'change'事件,但很容易添加:http://jsbin.com/yutuvi/2/edit –