【问题标题】:Backbone View: function is undefined骨干视图:功能未定义
【发布时间】:2014-10-12 19:38:08
【问题描述】:

我想构建一个简单的主干应用程序,用于通过 Stripe 存取资金。由于很多功能都很常见,因此我将其放置在 Stripe 视图中,并从中扩展了 Deposit 和 Withdraw 视图,如下所示:

var App = {
    Models: {},
    Collections: {},
    Views: {},
    Router: {}
}

App.Views.Home = Backbone.View.extend({
    el: $('#main-content'),

    template: Handlebars.compile($('#home-template').html()),

    render: function() {
        this.$el.html(this.template())
        return this
    },

    events: {
        'click #deposit-button': 'navigateToDeposit',
        'click #withdraw-button': 'navigateToWithdraw'
    },

    navigateToDeposit: function(e) {
        Backbone.history.navigate('/deposit', true)
    },

    navigateToWithdraw: function(e) {
        Backbone.history.navigate('/withdraw', true)
    }
})

App.Views.Stripe = Backbone.View.extend({
    el: $('#main-content'),

    initialize: function() {
        Stripe.setPublishableKey('pk_test_0QvQdPBkXAjB4EBsT4mf226x')
    },

    render: function() {
        this.$el.html(this.template())
        return this
    },

    events: {
        'click #submit': 'submitForm'
    },

    submitForm: function(e) {
        e.preventDefault()
        $('#submit').prop('disabled', true)
        var that = this
        Stripe.card.createToken($('#form'), that.stripeResponseHandler)
    },

    stripeResponseHandler: function(status, response) {
        var $form = $('#form')

        if(response.error) {
            $form.find('.payment-errors').text(response.error.message)
            $('submit').prop('disabled', false)
        } else {
            console.log(this)
            var form_data = this.getFormData(response.id),
                that = this
            $.post(that.transaction_endpoint, form_data, function(data, textStatus, jqXHR) {
                Backbone.history.navigate('/home', true)
            })
        }
    }
})

App.Views.Deposit = App.Views.Stripe.extend({

    template: Handlebars.compile($('#deposit-template').html()),

    getFormData: function(token) {
        return {
            amount: $('#form input[name=amount]').val(),
            token: token
        }
    },

    transaction_endpoint: 'handledeposit'
})

App.Views.Withdraw = App.Views.Stripe.extend({

    template: Handlebars.compile($('#withdraw-template').html()),

    getFormData: function(token) {
        return {
            name: $('#form input[name=name]').val(),
            email: $('#form input[name=email]').val(),
            token: token
        }
    },

    transaction_endpoint: 'handlewithdraw'
})

App.Router = Backbone.Router.extend({
    routes: {
        'deposit'   :       'showDepositView',
        'withdraw'  :       'showWithdrawView',
        '*path'     :       'showHomeView'
    },

    showDepositView: function() {
        var depositView = new App.Views.Deposit()
        depositView.render()
    },

    showWithdrawView: function() {
        var withdrawView = new App.Views.Withdraw()
        withdrawView.render()
    },

    showHomeView: function() {
        var homeView = new App.Views.Home()
        homeView.render()
    }
})

var router = new App.Router()

Backbone.history.start()

getFormData 方法的调用给了我一个错误,提示该函数未定义,即使我在存款和取款视图中都定义了它。另外,我在它的正上方添加了一个console.log(this),它将Window 对象记录到控制台而不是视图。我在这里做错了什么?

【问题讨论】:

    标签: javascript backbone.js backbone-views undefined-function


    【解决方案1】:

    我感觉这与这个电话有关:

    Stripe.card.createToken($('#form'), that.stripeResponseHandler)
    

    尝试使用.bind()this绑定到调用范围:

    Stripe.card.createToken($('#form'), that.stripeResponseHandler.bind(this))
    

    你真的不需要var that = this,但为了简单起见,我会留下它。

    【讨论】:

    • 我回家后试试这个。但从概念上讲,当我实际上想在 Deposit 和 Withdraw 视图的上下文中执行它时,不会在 Stripe 视图的上下文中执行 stripeResponseHandler 函数吗?
    • 我不这么认为(尽管我应该试试这个并给你一个明确的答案); Backbone 的 extend 方法在扩展视图时返回一个新对象,因此 this 应该是扩展视图
    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2014-01-04
    • 2012-07-17
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多