2013-05-31 6 views
8

私のアプリで何らかのエラーが発生しましたが、その理由はわかりません。Uncaught TypeError:未定義のメソッド 'on'を呼び出すことができません - Backbone.js

Uncaught TypeError: Cannot call method 'on' of undefined 

それはコードに従って、私のコレクションビューで発生:

App.WorkoutsView = Backbone.View.extend({ 
    initialize: function(){ 
     this.collection.on('add', this.addOne, this); 
     this.collection.on('reset', this.addAll, this); 
    }, 

    render: function() { 
     this.addAll(); 
     return this; 
    }, 

    addAll: function() { 
     this.$el.empty(); 
     this.collection.forEach(this.addOne, this); 
    }, 

    addOne: function(Workout) { 
     var workoutView = new App.WorkoutView({model: App.Workout}); 
     this.$el.append(workoutView.render().el); 
    } 
}); 

問題が上である:

this.collection.on('add', this.addOne, this); 

誰もがなぜ知っていますか?

**編集:コレクションコード**

App.WorkoutItems = Backbone.Collection.extend({ 
    model: App.Workout, 
    url: '/workouts', 

    localStorage: function() {new Backbone.LocalStorage('Workout')}, 

    initialize: function() { 
     this.on('remove', this.hideWorkout, this); 
    }, 

    hideWorkout: function() { 
     model.trigger('hide'); 
    }, 

    focusOnWorkoutItem: function() { 
     var modelsToRemove = this.filter(function(workoutItem) { 
      return workoutItem.id != id; 
     }); 
    this.remove(modelsToRemove); 
    } 
}); 

編集:私のルータのコード私はWorkoutsViewをインスタンス化しています:

App.Router = Backbone.Router.extend({ 
    routes: { 
     '':'index' 
    }, 

    initialize: function() { 
     this.workoutItems = new App.WorkoutItems(); 
     this.workoutsView = new App.WorkoutsView(); 
     this.workoutsView.render(); 
    }, 

    index: function() { 
     $('#workouts').html(this.workoutsView.el); 
     this.workoutsItem.fetch(); 
    } 
}); 

new App.Router; 
Backbone.history.start(); 
+0

「this.collection」とは何ですか? –

+1

ビューのインスタンス化に使用しているコードを表示できますか? –

+0

@ MarkLinusワークアウトコレクション、コード –

答えて

6
this.workoutsView = new App.WorkoutsView(); 

を渡すためにを想定していますコレクション

this.workoutsView = new App.WorkoutsView({collection : this.workoutItems}); 

初期化時には、ビューのInitializeメソッドでイベントをバインドしているときに、コレクションを使用できるようにする予定があるためです。

+1

を追加します@sushanth!どうもありがとうございました! –

+1

あなたは歓迎です:) –

関連する問題