2014-03-24 37 views
0

我試圖配置一個coffeescript類來管理分段付款。我正在基於this tutorial的解決方案。Uncaught TypeError:Object#<HTMLInputElement> has no method'processCard'

這裏是我的代碼:

class Subscription 

    count = 0 

    page = 
    createForm: "form#new_subscription" 
    createButton: 'input#create_subscription[type=submit]' 
    cardNumber: '#card_number' 
    cardCode: '#card_code' 
    cardMonth: '#card_month' 
    cardYear: '#card_year' 
    stripeError: '#stripe_error' 

    setupForm: -> 
    console.log "binding to submit" 
    $(page.createButton).click (e) -> 
     $(page.createButton).attr('disabled', true) 
     @processCard() 
     return false 

    processCard: -> 
    console.log "processing card" 
    card = 
     number: $(page.cardNumber).val() 
     cvc: $(page.cardCode).val() 
     expMonth: $(page.cardMonth).val() 
     expYear: $(page.cardYear).val() 

    Stripe.card.createToken(card, subscription.handleStripeResponse); 

    handleStripeResponse: (status, response) -> 
    if response.error 
     console.log "error" 
     $(page.stripeError).text(response.error.message) 
     $(page.createButton).attr('disabled', false) 
     alert ("bla") 
    else 
     alert("success: " + response.id) 

jQuery -> 
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) 
    subs = new Subscription 
    subs.setupForm() 

的問題是,當我點擊提交按鈕,我得到異常:

Uncaught TypeError: Object #<HTMLInputElement> has no method 'processCard' 

我相信這是由以下事實引起的通過點擊事件self調用的方法是觸發事件的html元素。而通過自我我需要JavaScript對象。

有關如何解決此問題的任何線索?

謝謝,

回答

1

沒有測試實際的代碼,它看起來像你有錯誤的範圍。

setupForm: -> 
    console.log "binding to submit" 
    $(page.createButton).click (e) => 
    $(page.createButton).attr('disabled', true) 
    @processCard() 
    return false 

通過在$(page.createButton).click (e) =>使用脂肪箭頭(=>)傳遞所述外範圍this回調內,然後可以調用@processCard()(其中@將指new Subscription實例和不jQuery的this參考其是HTMLInputElement )。

相關問題