2013-05-31 6 views
8

मुझे अपने ऐप में कुछ त्रुटि है और पता नहीं क्यों।Uncaught TypeError: अपरिभाषित 'ऑन' विधि को कॉल नहीं कर सकता - Backbone.js

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

किसी को भी जानता कि क्यों:

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

समस्या पर है:

Uncaught TypeError: Cannot call method 'on' of undefined 

यह मेरा संग्रह दृश्य पर होता है, कोड का पालन करें?

** संपादित करें: संग्रह कोड **

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 instantiating हूँ:

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

@ मार्किनस वर्कआउट संग्रह, मैं कोड –

उत्तर

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

माना जाता है एक में पारित करने के लिए संग्रह

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

क्योंकि जब आप प्रारंभ करते समय देखते हैं तो संग्रह को उपलब्ध होने की उम्मीद है क्योंकि आप दृश्य की प्रारंभिक विधि में किसी ईवेंट को बाध्य कर रहे हैं।

+1

जोड़ दूंगा जो यह @ सुशांत काम करता है! आपका बहुत बहुत धन्यवाद! –

+1

आपका स्वागत है :) –

संबंधित मुद्दे