2015-01-03 197 views
1

我的目標是計算基準價格和售價之間的保證金百分比。但我也希望能夠在保證金變化時重新計算價格。我不能僅僅通過property()觀察變化,因爲價格和利潤率將無限重新計算。相反,我想運行keyupchange事件的計算,這正是我如何在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

回答

1

這是相當容易擴展的文本字段組件支持key-up

App.KeyUpInputComponent = Em.TextField.extend({ 
    keyUp: function(event){ 
    // I chose to send these things, you could just send nothing 
    this.sendAction('key-up', event, this.get('value')); 
    } 
}); 


{{key-up-input value=asdf key-up='keyUpAction'}} 

http://emberjs.jsbin.com/didisuneka/1/edit?html,js,output

你jsbin

http://jsbin.com/xedaxageze/1/edit

errrr,我意識到我已經回答過了這一點,這就是生活... KeyPress event not working as expected in Ember

+0

其實我已經看到你的答案,但沒有理解如何將其應用於我的情況。感謝您用我的jsbin顯示它!我還需要處理'change'事件,但很容易添加:http://jsbin.com/yutuvi/2/edit –