view manager based on goog.ui.component for Backbone
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


a component handler for Backbone Views which derives heavily from with the selector conecpts from



The componentView has a few states. It starts out with no element, then createDom is called giving it an element and structure, and finally enterDocument will be called once it is put in the document. exitDocument will be called if it is removed from the document and you can also call dispose() to remove all it's references.

When a componentView is first instantiated it has no element. Calling createDom will create an element and try to call handlebars on this.template if one exists. If you want to create your DOM structure another way, just override createDom, or if you'd like to add to it you can use super like so:

var Red = Backbone.ComponentView.extend({
	createDom: function(force) {
		this.super('createDom', force);
		this.$el.css('background', 'red');

the enterDocument function is where you setup all you event handlers like listening to collections. If you setup anything here like references to DOM elements you'll want to clean them up in exitDocument (exitDocument by default will call undelegateEvents).

to put the view on a page you can do so like this:

var myRed = new Red({
	model: myModel;

render will append it to the document and call createDom and enterDocument.

you can also add children to the component using addChild or addChildAt and of course remove them with removeChild. When calling these methods you can optionally pass a true parameter that will render the child in to the component's content element or you can render them manually. Setting up a heirarchy like this enables the parents (that you can get with getParent) to control the enter and exit document methods when it is added or removed.

You can also use setView or insertView which will add a child in an element that matches a selector. you can use setViews that takes an object with keys as selector strings and values as either a componentView or another object to setup a heirarchy.


if you've extended a componentView you can call it's parent function with this.super(functionName, *args...);


if you're using Backbone.Advice you can get autolist functionality using this mixin:

function(options) {
    if (options.contentElement)
            getContentElement: function() {
                return this.$el.find(options.contentElement);

    this.after('enterDocument', function() {
        this.collection.on('reset', this.autolist_, this);
        this.collection.on('add', this.autolist_, this);
        this.collection.on('remove', this.autolist_, this);

        autolist_: function() {
            var model = this.collection;
            var models = model.models;
            var children = this._children || [];

            var childModels =, function(child) {
                return child.getModel();

            var removed = _.filter(children, function(child) {
                return !_.contains(models, child.getModel());

            if (children.length === 0) {
                _.each(models, function(model) {
                    var setup = _.extend({}, options.setup);
                    if (model instanceof Backbone.Model)
                        setup.model = model;
                    if (model instanceof Backbone.Collection)
                        setup.collection = model;
                    var newChild = new this.itemView(setup);
                    this.addChild(newChild, true);
              }, this);
            } else {
                _.each(removed, function(child) {
                    if (!_.chain(this.views).values().flatten().contains(child)) {
                        this.removeChild(child, true);
                }, this);
                _.each(models, function(model, ind) {
                    var child;
                    if(!_.contains(childModels, model)) {
                        var setup = _.extend({}, options.setup);
                        if (model instanceof Backbone.Model)
                            setup.model = model;
                        if (model instanceof Backbone.Collection)
                            setup.collection = model;
                        child = new this.itemView(setup);
                        this.addChildAt(child, ind, true);
                    } else {
                        child = _.find(children, function(c) {
                            return c.getModel() == model;
                        if (this.getChildAt(ind) != child)
                          this.addChildAt(child, ind);

                }, this);
        afterList: function() {},
        beforeList: function() {}



  • getChildrenBySelector should return a copy of the array


  • change exitDocument method to private so user can set


  • initial versioning