骨干视图 - 未定义

时间:2011-10-30 05:21:59

标签: javascript backbone.js

这真让我感到困惑,我觉得我很蠢,但我已经搜索过并尽我所能。 每当我声明一个视图并使用jasmine运行BDD测试时,它总是返回“undefined is not a function”。这是代码

window.LocationView = Backbone.View.extend({
    initialize: function() {
        // create new marker first
        this.marker = new google.maps.Marker({
            title: this.model.get('name'),
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: new google.maps.LatLng(this.model.get('lat'), this.model.get('long')), // give the position here
        });

        // bind events
        this.model.bind('change', this.render, this);
    },
    render: function() {
        this.marker.setTitle(this.model.get("name"));
        this.marker.setPosition(new google.maps.LatLng(this.model.get('lat'), this.model.get('long')));
    },
});

这是我宣布的方式:

this.view = new LocationView({model: this.location});
this.view = new LocationView();
// neither of these ones work.

使用jasmine运行此代码时出现错误:

TypeError: undefined is not a function
    at [object Object].make (http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js:29:37)
    at [object Object]._ensureElement (http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js:30:270)
    at [object Object].<anonymous> (http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js:28:127)
    at new <anonymous> (http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js:32:136)
    at [object Object].<anonymous> (http://localhost/gmap_api/public/test/spec/specs.js:62:21)
    at [object Object].execute (http://localhost/gmap_api/public/test/spec/jasmine/jasmine.js:1001:15)
    at [object Object].next_ (http://localhost/gmap_api/public/test/spec/jasmine/jasmine.js:1790:31)
    at [object Object].start (http://localhost/gmap_api/public/test/spec/jasmine/jasmine.js:1743:8)
    at [object Object].execute (http://localhost/gmap_api/public/test/spec/jasmine/jasmine.js:2070:14)
    at [object Object].next_ (http://localhost/gmap_api/public/test/spec/jasmine/jasmine.js:1790:31)

2 个答案:

答案 0 :(得分:18)

当我以错误的顺序包含我的javascript文件时,我遇到了类似的问题。像这样导入它们:

jQuery.js 
Underscore.js
Backbone.js
YourCode.js

如果情况并非如此,则发布发生此异常的行。

答案 1 :(得分:1)

您确定View的定义是在初始化代码之前吗?如果它位于单独的文件中,您确定首先执行View的定义吗?