关于knockout.js和范围的noob问题

时间:2012-05-04 17:20:57

标签: javascript knockout.js

跟着文档,尝试了我自己并遇到了一些问题。

initializeViewModel = function(){

    var listing_model = {
        sale_rent: ko.observable( jQuery('#id_sale_rent')),
        property_type: ko.observable( jQuery('#id_property_type').val()),
        address: ko.observable( jQuery('#id_address')),
        is_condo: ko.computed(function(){
            return this.property_type() == 'condominium';
        }, this)
    };



    listing_model.district = ko.computed(function(){

        return this.district() || this.property_type();
    }, listing_model);

    return listing_model;
}

语句return this.property_type() == 'condominium';导致异常object <object> has no method property_type()。我认为这可能是一个范围问题,但this似乎是指这里正确的实例。有人可以指出我的问题吗?

2 个答案:

答案 0 :(得分:2)

最干净的解决方案是使用匿名函数(创建闭包)而不是普通对象:

initializeViewModel = function(){
    var listing_model = new function() {
        // Close-in a reference to this object
        var self = this;

        self.sale_rent = ko.observable( jQuery('#id_sale_rent') );
        self.property_type = ko.observable( jQuery('#id_property_type').val() );
        self.address = ko.observable( jQuery('#id_address') );

        self.is_condo = ko.computed(function() {
            return (self.property_type() == 'condominium');
        });
    }();

    // ...

否则,函数内部的“this”(定义计算)指的是你作为第二个参数传递给ko.computed()的任何东西 - “this”的值是当前的上下文“initializeViewModel” “执行中,所以如果你像往常一样调用该函数(即initializeViewModel()),”this“将只是对全局对象的引用而不是”listing_model“(如预期/预期的那样)。 / p>

本手册中的示例与您的代码不同:您正在创建一个普通对象,而在手册中,所有内容都包含在一个函数中。使用“new”关键字调用该函数会创建一个新对象,并将上下文(“this”)设置为此对象。这就是他们的代码有效的原因。

答案 1 :(得分:0)

this指的是匿名函数作用域,其次this.property_type()是函数调用,你无法为其赋值。