ListenTo on collection reset is not called after collection is retrieved

Below is my view initialization function, which should get the module name during initialization to retrieve the correct data from the collection.

Problem:

  • Listento does not redirect the render () method after retrieving the collection, also it gives me an error on the console

TypeError: e - undefined

What mistake did I make with the code below?

    initialize: function() {          

     var that = this;
        if(this.options.module === 'questions'){                  

            require([
                'app/collections/questions'
            ], function(QuestionsCollection){                   
                var moduleCollection = new QuestionsCollection();
                that.collection = moduleCollection;
                moduleCollection.fetch({
                    reset: true, 
                    success: function(){},
                    error: function(){}
                    });                                                                        
            });                

        }

        this.listenTo(this.collection, 'reset', this.render);
        this.listenTo(Backbone, 'close:Home', this.close);
    },
+3
source share
2 answers

riquire.js, require([/*jsFile*/], callback);, callback jsFile. , require([... , this.listenTo(this.collection, 'reset', this.render);, jsFile , callback , this.collection - undefined.

http://jsfiddle.net/ZZuGC/1/, console.log('require'); console.log('listenTo'); .


, :

initialize: function() {          

 var that = this;
    if(this.options.module === 'questions'){                  

        require([
            'app/collections/questions'
        ], function(QuestionsCollection){                   
            var moduleCollection = new QuestionsCollection();

            that.collection = moduleCollection;
            // here is my suggestion
            that.listenTo(that.collection, 'reset', that.render);

            moduleCollection.fetch({
                reset: true, 
                success: function(){},
                error: function(){}
                });                                                                        
        });                

    }
    this.listenTo(Backbone, 'close:Home', this.close);
},

http://jsfiddle.net/ZZuGC/2/

+1

, , . :

initialize: function() {          

 var that = this;
    if(this.options.module === 'questions'){                  

        require([
            'app/collections/questions'
        ], function(QuestionsCollection){                   
            var moduleCollection = new QuestionsCollection();
            moduleCollection.fetch({
                reset: true, 
                success: function(){},
                error: function(){}
                });
            that.listenTo(moduleCollection, 'reset', that.render);                                                    
        });                
    }

    this.listenTo(Backbone, 'close:Home', this.close);  
},
+4

All Articles