Skip to content

Instantly share code, notes, and snippets.

@sarink
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarink/9014702 to your computer and use it in GitHub Desktop.
Save sarink/9014702 to your computer and use it in GitHub Desktop.
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define(["underscore", "backbone", "backboneBabySitter"], function(_, Backbone, ChildViewContainer) {
return factory(_, Backbone, ChildViewContainer);
});
}
else if (typeof exports !== "undefined") {
var _ = require("underscore");
var Backbone = require("backbone");
var ChildViewContainer = require("backboneBabySitter");
module.exports = factory(_, Backbone, ChildViewContainer);
}
else {
factory(root._, root.Backbone, root.ChildViewContainer);
}
}(this, function(_, Backbone, ChildViewContainer) {
"use strict";
var previousParentView = Backbone.ParentView;
Backbone.ParentView = (function(_, Backbone, ChildViewContainer) {
var ParentView = Backbone.View.extend({
// Initialize the `childViews` container and set `parentView` directly on the view if it's passed in via `options`
constructor: function constructor(options) {
_.extend(this, _.pick(options || {}, "parentView"));
this.childViews = new ChildViewContainer();
var result = Backbone.View.apply(this, arguments);
return result;
},
addChildView: function addChildView(child, options) {
options = _.defaults(options || {}, {append: true, render: true, customIndex: undefined});
child.parentView = this;
this.childViews.add(child, options.customIndex);
if (options.append) this.$el.append(child.el);
if (options.render) child.render();
this.trigger("addView", child, options);
return child;
},
createChildView: function createChildView(modelOrCollection, ChildViewType, childViewOptions, options) {
var obj = {};
if (modelOrCollection) {
if (modelOrCollection instanceof Backbone.Model) obj = {model: modelOrCollection};
else if (modelOrCollection instanceof Backbone.Collection) obj = {collection: modelOrCollection};
}
obj.parentView = this;
return this.addChildView(new ChildViewType(_.extend(obj, childViewOptions)), options);
},
removeChildView: function removeChildView(child) {
if (child) {
this.childViews.remove(child);
child.remove(_.tail(arguments));
this.trigger("removeView", child);
}
return child;
},
removeAllChildViews: function removeAllChildViews() {
var args = arguments;
return this.childViews.each(function(view) { this.removeChildView(view, args); }, this);
},
appendAllChildView: function appendAllChildView() {
return this.childViews.each(function(view) { this.$el.append(view.el); }, this);
},
renderAllChildViews: function renderAllChildViews() {
return this.childViews.apply("render", arguments);
},
remove: function remove() {
this.removeAllChildViews(arguments);
return Backbone.View.prototype.remove.call(this, arguments);
}
});
return ParentView;
})(_, Backbone, ChildViewContainer);
Backbone.ParentView.VERSION = "0.1";
Backbone.ParentView.noConflict = function() {
Backbone.ParentView = previousParentView;
return this;
};
return Backbone.ParentView;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment