2011-10-30 62 views
8

這確實讓我困惑,我覺得我傻,但我已經搜查,已經做了一切我所能。 每當我聲明一個視圖並用茉莉花運行BDD測試時,它總是返回「未定義不是函數」。這是代碼骨幹觀點 - 沒有定義

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. 

這是錯誤,當我運行此代碼茉莉:

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) 
+0

我不確定「聲明視圖然後聲明新視圖」是什麼意思。你實例化一個LocationView,然後是第二個LocationView嗎?第一個很好,但第二個不是?或者'新的LocationView({model:this.location})'工作,但'新的LocationView();'不? –

+0

@ muistooshort:對不起,我修好了。這意味着它會提示我LocationView不是每次都定義,儘管我聲明瞭它? – nXqd

回答

18

我有一個類似的問題時,我包括在錯誤的順序我的JavaScript文件。像這樣導入它們:

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

如果不是這種情況,則發佈發生此異常的行。

+0

這不解決我的問題。我重新用茉莉花此刻我的項目。但是,這看起來像最好的答案在這裏。感謝 – nXqd

+1

你應該包括骨幹網的開發版本,準確地看到在化妝函數你會得到錯誤,當Backbone嘗試爲你的視圖創建一個HTML元素時http://documentcloud.github.com/backbone/docs/backbone.html#section-117它看起來像你沒有$可用時您正在運行測試 – dira

+0

感謝您的幫助,迪拉:) – nXqd

1

確定視圖的定義初始化代碼之前?如果它在一個單獨的文件中,你確定View的定義是先執行的嗎?

+0

當我聲明新模型的定義並調用它時。它工作正常:( – nXqd