Javascript backbone.js此[method]不是函数

Javascript backbone.js此[method]不是函数,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我的Backbon.js视图中的helper函数有问题。当它运行时,它将终止,并显示关于“addCalc”函数第一行的以下错误消息: TypeError:this.getCalcValue不是函数 这确实令人费解,因为在上面定义的“initialize”函数中,所有函数似乎都已定义。感觉我调用同级方法是错误的,“initialize”方法是一个例外,其中“this”可以用来引用对象 以下代码是否有错误/缺失,或者主干文档是否有遗漏 CalcView = Backbone.View.extend(

我的Backbon.js视图中的helper函数有问题。当它运行时,它将终止,并显示关于“addCalc”函数第一行的以下错误消息:

TypeError:this.getCalcValue不是函数

这确实令人费解,因为在上面定义的“initialize”函数中,所有函数似乎都已定义。感觉我调用同级方法是错误的,“initialize”方法是一个例外,其中“this”可以用来引用对象

以下代码是否有错误/缺失,或者主干文档是否有遗漏

CalcView = Backbone.View.extend({
    el: $("#calcView"),
    initialize: function () {
        this.resetCalc();
    },

    addCalc: function (model) {
        var cost = this.getCalcValue(model.get('currentCost'));
        var custom = this.getCalcValue(model.get('customProgram'));

        var variables = { id: model.get('id'),
                category: model.get('category'),
                shortDesc: model.get('shortDescription'),
                description: model.get('description'),
                currentCost: cost,
                customProgram: custom,
        };

        var template = _.template($('#calc_template').html(), variables);
        $("#calc_payload").append(template);
    },

    resetCalc: function(models) {
        $("#calc_payload tr").remove();
    },

    removeCalc: function(model){
        $("#calc_payload #" + model.get('id')).remove();
    },

    updateCalcs: function(model) {

        var cost = model.get('currentCost');
        var custom = model.get('customProgram');

        $("#" + model.get("id") + " .currentCost").text(this.getCalcValue(cost));
        $("#" + model.get("id") + " .customProgram").text(this.getCalcValue(custom));

        /*var currentCostSum = 0;
        var customProgramSum = 0;
        $("#calc_payload .currentCost").each(function() {
            var temp = Number(($(this).text()).replace(/[^0-9\.]+/g, ""));
            if (!isNaN(temp))
                    currentCostSum += temp;
        });

        $("#calc_payload .customProgram").each(function() {
            var temp = Number(($(this).text()).replace(/[^0-9\.]+/g, ""));
            if (!isNaN(temp))
                customProgramSum += temp;
        });

        $("#calc_footer .currentCost").text("$" + ((currentCostSum == 0) ? " -- " : CurrencyFormatted(currentCostSum.toFixed(2))));
        $("#calc_footer .customProgram").text("$" + ((customProgramSum == 0) ? " -- " : CurrencyFormatted(customProgramSum.toFixed(2))));*/
    },

    getCalcValue: function(value) {
        if (typeof value == 'string' || value instanceof String)
            return value.toString();
        else if (isNaN(value))
            return "$ -- ";
        else
            return "$" + value.toFixed(2); 
    },                  
});
执行“addCalc”函数的代码由主干集合驱动。基本上,当集合添加到时,将调用CalcView.addCalc

Calculations = Backbone.Collection.extend({
    model: Calculation,
    //This is our Friends collection and holds our Friend models
    initialize: function (models, options) {
        this.on("add", options.onAdd);
        this.on("remove", options.onRemove);
        this.on("reset", options.onReset);
        //Listen for new additions to the collection and call a view function if so
    }
});


//This is where addCalc is used.
var calcview = new CalcView();
var calc_collection = new Calculations( null, { 
    onAdd: calcview.addCalc, 
    onRemove: calcview.removeCalc, 
    onReset: calcview.resetCalc 
});

initialize
函数中添加以下代码行:

_.bindAll(this,'addCalc');
这将把
This
绑定为
addCalc
函数的
CalcView
。如果需要绑定多个函数,可以在其中放置多个逗号分隔的方法名


请参见

当您在
集合
上绑定事件时,您可以将
上下文
作为第三个参数发送。尝试将另一个选项属性作为
calcview
发送并作为上下文传递

this.on("add", options.onAdd, options.calcview);
this.on("remove", options.onRemove, options.calcview);
this.on("reset", options.onReset, options.calcview);

请发布调用
addCalc
函数的代码。我用调用addCalc方法的文章更新了帖子。您的事件是如何绑定的?仔细研究,我发现“this”当前绑定到了集合对象,当元素添加到集合中时,该集合对象正在调用“addCalc”方法。