最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - backbone view - is not defined - Stack Overflow

programmeradmin1浏览0评论

This really confuses me, I think I'm stupid but I've searched and have done whatever I could. Whenever I declare a view and run BDD test with jasmine, it always returns "undefined is not a function". This is code

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 is how I declared it :

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

This is the error when I run this code with jasmine:

TypeError: undefined is not a function
    at [object Object].make (.js/0.5.3/backbone-min.js:29:37)
    at [object Object]._ensureElement (.js/0.5.3/backbone-min.js:30:270)
    at [object Object].<anonymous> (.js/0.5.3/backbone-min.js:28:127)
    at new <anonymous> (.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)

This really confuses me, I think I'm stupid but I've searched and have done whatever I could. Whenever I declare a view and run BDD test with jasmine, it always returns "undefined is not a function". This is code

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 is how I declared it :

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

This is the error when I run this code with 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)
Share Improve this question edited Oct 30, 2011 at 7:32 Tzu ng asked Oct 30, 2011 at 5:21 Tzu ngTzu ng 9,24414 gold badges68 silver badges106 bronze badges 2
  • I'm not sure what "declare a view and then declare a new view" means. Are you instantiating one LocationView and then a second LocationView? The first one is fine but the second one isn't? Or does new LocationView({model: this.location}) work but new LocationView(); doesn't? – mu is too short Commented Oct 30, 2011 at 5:48
  • @muistooshort: Sorry for bad English, I fix it. It means it prompts me LocationView is not defined everytime, although I declared it ?? – Tzu ng Commented Oct 30, 2011 at 7:18
Add a comment  | 

2 Answers 2

Reset to default 18

I had a similar problem when I included my javascript files in the wrong order. Import them like so:

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

If that's not the case then post the line at which this exception occurs.

Are you sure the View's definition is before the initialization code? If it's in a separate file, are you sure the View's definition is executed first?

发布评论

评论列表(0)

  1. 暂无评论