Code coverage report for src/marionette.itemview.js

Statements: 100% (24 / 24)      Branches: 100% (6 / 6)      Functions: 100% (3 / 3)      Lines: 100% (23 / 23)     

All files » src/ » marionette.itemview.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66            1                 114   114 70   44 2     114                 114   114 114   114 114   114 114   113 113   113 113   113           88   86   86   86      
// Item View
// ---------
 
// A single item view implementation that contains code for rendering
// with underscore.js templates, serializing the view's model or collection,
// and calling several methods on extended views, such as `onRender`.
Marionette.ItemView =  Marionette.View.extend({
 
  // Serialize the model or collection for the view. If a model is
  // found, `.toJSON()` is called. If a collection is found, `.toJSON()`
  // is also called, but is used to populate an `items` array in the
  // resulting data. If both are found, defaults to the model.
  // You can override the `serializeData` method in your own view
  // definition, to provide custom serialization for your view's data.
  serializeData: function(){
    var data = {};
 
    if (this.model) {
      data = this.model.toJSON();
    }
    else if (this.collection) {
      data = { items: this.collection.toJSON() };
    }
 
    return data;
  },
 
  // Render the view, defaulting to underscore.js templates.
  // You can override this in your view definition to provide
  // a very specific rendering for your view. In general, though,
  // you should override the `Marionette.Renderer` object to
  // change how Marionette renders views.
  render: function(){
    this.isClosed = false;
 
    this.triggerMethod("before:render", this);
    this.triggerMethod("item:before:render", this);
 
    var data = this.serializeData();
    data = this.mixinTemplateHelpers(data);
 
    var template = this.getTemplate();
    var html = Marionette.Renderer.render(template, data);
 
    this.$el.html(html);
    this.bindUIElements();
 
    this.triggerMethod("render", this);
    this.triggerMethod("item:rendered", this);
 
    return this;
  },
 
  // Override the default close event to add a few
  // more events that are triggered.
  close: function(){
    if (this.isClosed){ return; }
 
    this.triggerMethod('item:before:close');
 
    Marionette.View.prototype.close.apply(this, slice(arguments));
 
    this.triggerMethod('item:closed');
  }
});