0

Why this scope is not working? Why do I need to make temp var in below code.

I have tried to render with this but object is undefined. Though I have achieved the results but I do not understand why I need to temp variable named as that.

var CategoryView = Backbone.View.extend({
    template: require('hbs!./../templates/CategoryView'),
    render: function () {
        var that = this;
        this.collection.fetch({
            contentType: 'application/json',
            type: 'GET',
            success: function(categoryCollection) {
                that.$el.html(that.template(categoryCollection.toJSON()));
            }
        });
        return this;
    }
});
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Waseem Saeed
  • 85
  • 1
  • 13
  • While the dupe will certainly help you understand the concept, Backbone and Underscore often offer a `context` parameter. `this.collection.fetch({ success: function(){/*..*/}, context: this })`. – Emile Bergeron Sep 28 '17 at 14:06
  • Also common with models and collections is to [`listenTo`](http://backbonejs.org/#Events-listenTo) the `"sync"` event. Multiple views can listen to the event so that they don't need to call `fetch` themselves. With Backbone, you'll rarely need `.bind` for this reason. – Emile Bergeron Sep 28 '17 at 14:10
  • Also, you don't need to pass `contentType` and `type` to `fetch` as it's already the default values. – Emile Bergeron Sep 28 '17 at 14:16

0 Answers0