2012-12-12 44 views
0

我是Backbone.js的新手,如果我不夠具體,請分析,以便道歉。Parse.js綁定錯誤

我有一個視圖,在初始化時拋出一個TypeError,即使我通過刪除問題行修復,只是拋出另一個完全可以接受的代碼錯誤。

以下是錯誤我得到:

Uncaught TypeError: Cannot call method 'bind' of undefined views.js:59 
Uncaught TypeError: Cannot call method 'bind' of undefined views.js:59 

下面是這個視圖的初始化函數,線59和60:

initialize: function() { 
    this.model.bind("reset", this.render, this); 
    this.model.bind("add", this.appendNewJob, this); 
} 

請讓我在評論中知道,如果你需要更多的代碼來繼續。 更新:根據要求,我的渲染和appendNewJob功能:

render: function(eventName) { 
    _.each(this.model.models, function(job){ 
     this.appendNewJob(job); 
    }, this); 
    this.delegateEvents(); 
    return this.el; 
}, 
appendNewJob: function(job){ 
    this.$el.append(new JobListItemView({ 
     model: job 
    }).render()); 
} 

我的路由器:

var AppRouter = Parse.Router.extend({ 
    initialize: function() { 
     $('#header').html(new HeaderView().render()); 
    }, 
    routes: { 
     "": "list", 
     "jobs": "list", 
     "settings": "viewSettings" 
    }, 
    list: function() { 
     var self = this; 
     FB.getLoginStatus(function(response){ 
      if(response.status === 'connected') { 
       // logged in. 
       self.before(function() { 
        self.showView('#content', new JobListView()); 
       }); 
      } else if(response.status === 'not_authorized') { 
       // not authorized. 
      } else { 
       // not connected. 
       $('#app').hide(); 
       self.before(function() { 
        self.showView('#login', new StartView()); 
       }); 
      } 
     }); 
    }, 
    viewSettings: function() { 
     var self = this; 
     FB.getLoginStatus(function(response){ 
      if(response.status === 'connected') { 
       // logged in. 
       self.before(function() { 
        self.showView('#content', new SettingsView()); 
       }); 
      } else if(response.status === 'not_authorized') { 
       // not authorized. 
      } else { 
       // not connected. 
       $('#app').hide(); 
       self.before(function() { 
        self.showView('#login', new StartView()); 
       }); 
      } 
     }); 
     $('#filterBar').hide(); 
    }, 
    showView: function(selector, view) { 
     if(this.currentView) this.currentView.close(); 
     $(selector).html(view.render()); 
     this.currentView = view; 
     return view; 
    }, 
    before: function(callback) { 
     if(this.jobList) { 
      if(callback) callback.call(this); 
     } else { 
      this.jobList = new JobCollection(); 
      var self= this; 
      this.jobList.fetch({ 
       success: function() { 
        var joblist = new JobListView({ 
         model: self.jobList 
        }).render(); 
        $('#content').html(joblist); 
        if(callback) callback.call(self); 
       } 
      }); 
     } 
    } 
}); 

UPDATE:我也是用解析JS庫到位骨幹。

+1

你一定要貼上更多的代碼進行初始化。 – soulcheck

+0

你可以粘貼代碼初始化視圖..可能你的'路由器' –

+0

我剛剛修改了這個問題,因爲我忘了補充說我使用的是Parse庫而不是backbone.js。 – liquidus219

回答

1

您正在初始化您的視圖,但未傳入model選項。該錯誤是一個標準的JavaScript錯誤,它告訴你上線59 this.model未定義:

this.model.bind("reset", this.render, this); 

視圖不能有this.model財產,除非你給模型視圖!

所以不是:

new JobListView(); 

您需要

new JobListView({model: <insert your model here>}); 
+0

工作,謝謝。 – liquidus219