2013-05-31 25 views
8

Uygulamamda bir hata yaşıyorum ve nedenini bilmiyorum.Yakalanmamış TypeError: Tanımlanmamış 'on' yöntemi aranamaz - Backbone.js

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

Herkes neden biliyor:

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); 
    } 
}); 

sorun olduğunu:

Benim toplama görünümde olur
Uncaught TypeError: Cannot call method 'on' of undefined 

, kodunu takip?

** Düzenleme: Koleksiyon kodu **

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); 
    } 
}); 

Düzenleme: i WorkoutsView başlatmasını ediyorum benim Yönlendirici kodu:

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

Tam olarak ne this.collection' nedir? –

+1

Görünümü oluşturmak için kullandığınız kodu gösterebilir misiniz? –

+0

@MarkLinus Egzersiz koleksiyonu, kod ekleyeceğim –

cevap

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

bir geçmek gerekiyordu koleksiyon

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

Başlatıldığında, görünümü olayın Initialize yönteminde bir olaya bağladığınız için bir koleksiyona sahip olmasını beklediğinizi görüntülediğiniz için.

+1

çalışır Sushanth! Çok teşekkür ederim! –

+1

Rica ederim :) –

İlgili konular