Luca.View

The Luca.View class is the base class for Luca components. A number of patterns and optimizations that are helpful in your view classes have been extracted into the base class.

Hooks

The concept of hooks in Luca is that components can trigger events, and we can bind to them as normally, and that this is good. However, where it is more useful or cleaner to just define methods that represent some part of the component lifecycle, we provide a configuration API for doing that.

  _.def("Luca.View").extends("Backbone.View").with
    hooks:[
      "after:initialize"   # => @afterInitialize
      "before:render"      # => @beforeRender
      "after:render"       # => @afterRender
      "first:activation"   # => @firstActivation
      "activation"         # => @activation
      "deactivation"       # => @deactivation
    ]

Any Luca.View which defines an @afterInitialize method, or a @beforeRender method, will automatically call that method when the corresponding event is triggered.

Note on extending hook methods

If you want to maintain the functionality of the component you are extending from, you will have to remember to call the prototype method like such:

  _.def("MyView").extends("Luca.View").with
    beforeRender: ()->
      @_super("beforeRender", @, arguments)

      # or, if you prefer 
      Luca.View::beforeRender?.apply(@, arguments)

The @render() method

The default implementation of @render() simply appends the view's @$el to the DOM element represented by the $(@container) property on the view.

Whatever method you choose to implement for your @render() call should behave similar to how the Backbone.View::render() expects, in that it should return an instance of the view.

Additionally, the call to @render() will trigger before:render and after:render as which, on a Luca.View is configured as a hook. So any @beforeRender() and @afterRender() method will get called as well, if they exist.

Before Render

Since in Luca, the actual render method just attaches the view to its container, setup related methods for building your view's content are best put in the beforeRender() method. In addition to this, there are other options available for filling the content of the view, like @bodyTemplate.

Luca.template helper

Luca.template() is a util function which allows you reference your client side template system. It accepts a name of a template ( which, if not found, it will attempt to match one for you ) and an object of interpolations to pass to the template function

Luca.available_templates() is a util function, useful for debugging, to see which templates are available to you.

Configuration Options

  view = new Luca.View(name:"my_view")

  Luca("my_view") is view # => true

Luca.View::$bodyEl()

In your Luca.View definitions, If you set the @bodyTemplate property to one of the available templates, then on initialize the view will set the HTML of its DOM element to the contents of the template.

This is useful in cases where there is a fair amount of structural, or otherwise static DOM content in your view, and one of the standard Luca.core.Container components is not suited for what you want to do. The Luca.components.Panel view is basically just a Luca.View which has additional DOM containers for footers and headers. It accomplishes this through the use of the @bodyClassName and @bodyTagName properties.

@bodyClassName and @bodyTagName work the same way the @className and @tagName properties work on standard Backbone views. They are used to create a DOM element via Backbone.View.prototype.make(@bodyTagName,class:@bodyClassName,id:@bodyId

If you use view.$bodyEl() instead of the standard view.$el() that ships with Backbone, all of the standard DOM manipulation methods available will be scoped to the CSS selector that corresponds to the actual body element of your view.

Deferrable Rendering

The jury is still out as to whether or not deferrable rendering is a useful pattern, or whether it is too complex. The use case it was trying to optimize is for views which can only be rendered in response to an event being fired on another object. Such as Backbone.Collection::fetch.

If this is what you are doing, then this feature is for you.

The options available for views which use the @deferrable property are as follows:

If you set @deferrable to true then the view will expect a @collection property.

A useful example would be for views which get rendered hidden, and activated if and only if the user does a specific action. ( For example, a TabView activating a secondary tab ). If that action triggers an event, and you want to delay the render process if and only if that event is triggered.

Helpers

Backbone Component Helpers

Views which have properties on them referencing other views, models, or collections, can access those objects by calling view.models() or view.views() or view.collections(). This is mainly useful for introspection, debugging, or what not.