Question

I think this source code speaks for itself, it creates a new view. Every time I find a tutorial there is this return value on the render function. But I never understood what it does, my code always worked with or without that return value, what does it exactly do?

  // renders individual todo items list (li)
    app.TodoView = Backbone.View.extend({
      tagName: 'li',
      template: _.template($('#item-template').html()),
      render: function(){
        this.$el.html(this.template(this.model.toJSON()));
        return this; // "enable chained calls" whatever that means
      }
    });
Was it helpful?

Solution

It does what you say, it enables chain calls that means you can invoke multiple method calls.

The most common use I've seen in Backbone it's calling el after render.

var view = new TodoView();
('#todo').append(view.render().el);

But you could create any other method in your view and chain it together.

view.render().method1().method2()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top