backbone.js这个。[方法]不是一个函数

时间:2013-09-18 14:13:21

标签: javascript jquery backbone.js

我的Backbon.js视图中有一个辅助函数问题。当它运行时,它会因“addCalc”函数的第一行出现以下错误消息而死:

  

TypeError:this.getCalcValue不是函数

这真的令人费解,因为在上面定义的“初始化”函数中,所有函数似乎都被定义了。感觉我正在调用兄弟方法错误,而“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 
});

2 个答案:

答案 0 :(得分:2)

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

_.bindAll(this,'addCalc');

这会将this绑定为CalcView addCalc函数的{{1}}。如果需要绑定多个函数,可以在其中放置多个逗号分隔的方法名称...

请参阅Underscore's documentation on it here.

答案 1 :(得分:0)

绑定collection上的事件时,您可以发送context作为第三个参数。尝试再发送一个选项属性作为calcview并将其作为上下文传递。

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