Sha256: b11645b700f8a08ca1306e1d3e2ba58d820a726e5dfbe82ddb2eb57d56974fad

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

Support.CompositeView = function(options) {
  this.children = _([]);
  Backbone.View.apply(this, [options]);
};

_.extend(Support.CompositeView.prototype, Backbone.View.prototype, Support.Observer.prototype, {
  leave: function() {
    this.trigger('leave');
    this.unbind();
    this.unbindFromAll();
    this.remove();
    this._leaveChildren();
    this._removeFromParent();
  },

  renderChild: function(view) {
    view.render();
    this.children.push(view);
    view.parent = this;
  },
  
  renderChildInto: function(view, container) {
    this.renderChild(view);
    this.$(container).empty().append(view.el);
  },

  appendChild: function(view) {
    this.renderChild(view);
    $(this.el).append(view.el);
  },
  
  appendChildTo: function (view, container) {
    this.renderChild(view);
    this.$(container).append(view.el);
  },
  
  prependChild: function(view) {
    this.renderChild(view);
    $(this.el).prepend(view.el);
  },
  
  prependChildTo: function (view, container) {
    this.renderChild(view);
    this.$(container).prepend(view.el);
  },

  swapped: function () {
    this.trigger('swapped')
  },

  _leaveChildren: function() {
    this.children.chain().clone().each(function(view) {
      if (view.leave)
        view.leave();
    });
  },

  _removeFromParent: function() {
    if (this.parent)
      this.parent._removeChild(this);
  },

  _removeChild: function(view) {
    var index = this.children.indexOf(view);
    this.children.splice(index, 1);
  }
});

Support.CompositeView.extend = Backbone.View.extend;

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backbone-support-0.4.0 lib/assets/javascripts/backbone-support/composite_view.js
backbone-support-0.3.2 lib/assets/javascripts/backbone-support/composite_view.js