17

In general, how to access parent view from a child view in Backbone?

Specifically, in Backgrid.js, is there a way to access parent row from a cell?

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

30

Pass this as an option to the child view on initialization step:

var ChildView = Backbone.View.extend({
  initialize : function (options) {
    this.parent = options.parent;
  }
});

// somewhere in the parent view ...
new ChildView({parent:this});
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
-2
// You can use this code instead

var ChildView = Backbone.View.extend({
  initialize : function (options) {
    this._configure(options); // Set all the options as local variables
    // This is used in the Backbone.View code on the latest version

  }
});
Karla
  • 1
  • Don't see `_configure` anywhere in the Backbone source. I do see `_.extend(this, _.pick(options, viewOptions));` though. In this case, it would be used as follows: `_.extend(this, _.pick(options, ['parent']));` – Philip Ramirez Apr 27 '14 at 21:17