Barefoot.View

The barefoot view extends the default backbone view by applying an environment specific mixin to it.

For further information, please refer to the regarding environment specific mixins.

Environment Specific Mixins

Example View Implementation

var MainView = Barefoot.View.extend({
    el: 'body'
    , initialize: function() {
        this.addSubview(new NavigationView());
    }
    , template: '<header><h1>Application</h1><nav></nav></header>'
              + '<section id="main"></section>';
    , renderView: function() {
        this.$el.html(this.template);
    }
    , events: {
        'click h1': 'onClickTitle'
    }
    , onClickTitle: function onClickTitle(e) {
        console.log('Yay! Title clicked.')
    }
});

The following sections discuss specific aspects / differences of this code snippet in comparison to a common Backbone application.

Where’s the render() function?

Working with Barefoot.View, you should never implement a render function.  Instead, do everything you’d do there inside of the renderView function.  Barefoot overwrites backbones render() with its own version (see <Barefoot.View.Shared.render>) to accomplish hassle free view rendering, both on client and server.

Subviews/Nested Views

You may be used to create the subviews of your views directly in the render function and call their render function there.

Barefoot supports you by providing the addSubview and removeSubview functions.  Use these functions inside the initialization function of your view.  Managing subviews this way brings a few improvements:

  • Barefoot can render views on its own on the server and the browser client
  • You do not take care of destroying your view hierarchy when rendering a new view.  Barefoot will handle this for you.  (No more Zombies)

beforeRender and afterRender hooks

Barefoot looks for a beforeRender or afterRender function and executes them before / after rendering your view automatically.

If beforeRender or afterRender is invoked, a “resolve” and “rejected” argument is passed to them.  These two functions are essential when using asynchronous calls which prepare the view for rendering or doing async cleanup work.

Lets say you use the beforeRender hook to load some data from your APIAdapter into Models.  Since calls to the APIAdapter are async, you have to ensure that beforeRender does not finish until the api call is done and your data in place.  The following example shows such a beforeRender implementation:

function beforeRender(resolve, reject) {
    contactsCollection.fetch({
        success: function() {
            resolve();
        }
    });
    console.log('Loading contacts from API...');
}

This function will not “terminate” as soon as the console.log statement was executed.  Barefoot will wait until you explicitly call the “resolve” function argument (or reject in case something went wrong).

These technique is widely known as “Promises”.  Barefoot uses the popular implementation Q implementation of the Promises/A+ spec.

Attention

Please be aware you are not overwriting the renderSubviews and render method.  This would break the rendering mechanisms of Barefoot on the server and client.

Summary
Barefoot.ViewThe barefoot view extends the default backbone view by applying an environment specific mixin to it.
Functions
addSubviewAdds a Barefoot.View as subview of this view.
removeSubviewRemoves Barefoot.View from this view (if present as subview) and sets the former subviews parentView property to undefined.
renderSubviewsIterates each present subview of this view and renders it to the DOM.
renderOverwrites the default Backbone.View.render method.
selectDOMElementTries to select a DOM element for this View using the passed DOM manipulator that confirms to the jQuery API (http://api.jquery.com/).

Functions

addSubview

function addSubview(subview)

Adds a Barefoot.View as subview of this view.  Sets the parentView property of the subview to this view.

Parameters

(Barefoot.View) subviewBarefoot.View to add as subview to this view

removeSubview

function removeSubview(subview)

Removes Barefoot.View from this view (if present as subview) and sets the former subviews parentView property to undefined.

Parameters

(Barefoot.View) subviewA Barefoot.View subview to remove from this view

renderSubviews

function renderSubviews()

Iterates each present subview of this view and renders it to the DOM.  Ensures that all events of the subviews are bind to the DOM.

Attention

Do never call this method on your own.  <Barefoot.View.Shared.render> invoces this method automatically when needed.

render

function render()

Overwrites the default Backbone.View.render method.  It automatically invokes “renderView” and renderSubviews.

If you define a beforeRender or afterRender function in your View implementation, that function will be called before / after the actual rendering takes / took place.

Attention

  • Do not overwrite this method as you are used to from “normal” Backbone view implementations.  Create a renderView method in your view instead!
  • Make sure you implement a renderView() function for your concrete view.

See also

selectDOMElement

function selectDOMElement($)

Tries to select a DOM element for this View using the passed DOM manipulator that confirms to the jQuery API (http://api.jquery.com/).

Parameter

(Object) $A DOM manipulator that confirms to the jQuery API

Results

(Object)

function addSubview(subview)
Adds a Barefoot.View as subview of this view.
function removeSubview(subview)
Removes Barefoot.View from this view (if present as subview) and sets the former subviews parentView property to undefined.
The barefoot view extends the default backbone view by applying an environment specific mixin to it.
function renderSubviews()
Iterates each present subview of this view and renders it to the DOM.
function render()
Overwrites the default Backbone.View.render method.
function selectDOMElement($)
Tries to select a DOM element for this View using the passed DOM manipulator that confirms to the jQuery API (http://api.jquery.com/).
This mixin contains client specific code for the Barefoot.View class.
Enhances the Barefoot.View class with server specific code fragments.
When building a fancy JavaScript based client you’ll probably have a straight forward REST API in the backend.
For the moment, the Barefoot model does not introduce any new functionalities to Backbones model.
Close