Skip to content
bobholt edited this page Apr 10, 2013 · 4 revisions

BaseView is the foundation for the building of Backbone Views for our applications. It includes useful properties and methods that encapsulate functionality that tends to be needed over and over again. It owes a great debt to the SuperView presented by Rebecca Murphey at BackboneConf 2012.

Methods and properties on BaseView as of Keel 0.1 are as follows:

  • elements (property)

    The elements array allows us to reference elements from our View templates from within our View. This is especially useful in that it allows us to place child Views within a parent View prior to appending it to the DOM.

  • template (property)

    The template object is a placeholder that is used in the absence of a template explicitly-defined within concrete Views. Our default render method expects a template, so this placeholder prevents errors from occurring when initially creating a View. Each concrete View should implement its own template.

  • constructor (method)

    We override the constructor method to add a children instance property so that each View has reference to all of its children Views. This allows the parent to destroy its children when it itself is destroyed.

  • addChild (method)

    This method allows us to attach another View as a child of the current View. We pass an object into this method that acts as a child specification. This specification includes a reference to the View constructor for the child, a unique ID which serves as the key in the parent's list of its children, the element to which to attach the child View, and a Backbone View options object that is passed directly into the View instantiation (e.g. new ChildView( childSpec.options );).

  • addChildren (method)

    This method accepts an array of specification objects, loops through them, and calls addChild on each one in order.

  • destroy (method)

    This method destroys all children of the View and then calls the Backbone remove method, which removes the View from the DOM and calls stopListening to unbind events.

  • destroyChild (method)

    This method finds the reference to the child View (by unique ID) in the map created in constructor, calls destroy on it, and removes the reference.

  • destroyChildren (method)

    This method loops through an array of child View IDs, and calls destroyChild on each one.

  • getTemplate (method)

    This method is used by the render method. It takes care of caching or pre-rendering the template described in the View's template property.

  • place (method)

  • postPlace (method)

  • postRender (method)

  • render (method)

  • _setupElements (method)

Clone this wiki locally