/api/item-view.yaml

https://gitlab.com/CodeYellowBV/backbone.marionette · YAML · 169 lines · 128 code · 41 blank · 0 comment · 0 complexity · 7f99e3a166baec86cd4a24297080affc MD5 · raw file

  1. name: ItemView
  2. class: Marionette.ItemView
  3. extends:
  4. - Marionette.View
  5. description: |
  6. An `ItemView` is a view that represents a single item. That item may be a `Backbone.Model` or may be a `Backbone.Collection`. Whichever it is though, it will be treated as a single item.
  7. Please see [the Marionette.View documentation](marionette.view.md) for more information on available features and functionality.
  8. Additionally, interactions with Marionette.Region will provide features such as `onShow` callbacks, etc. Please see [the Region documentation](marionette.region.md) for more information.
  9. constructor:
  10. description: |
  11. Creates a new ItemView.
  12. As a View, the constructor calls the initialize function if it exists.
  13. @param {Object} options
  14. examples:
  15. -
  16. name: Creating a new ItemView representing a single model.
  17. example: |
  18. ItemViews can be used to represent either a `Backbone.Model` or `Backbone.Collection` by setting `model` or `collection` properties on the ItemView. The collection is only used if a model is not already set.
  19. ```js
  20. var MyItemView = Marionette.ItemView.extend({
  21. template: '#some-item-template'
  22. });
  23. var itemView = new MyItemView({
  24. model: myModel
  25. });
  26. ```
  27. functions:
  28. serializeData:
  29. description: |
  30. Serialize the model or collection for the view. If a model is found, the view's `serializeModel` is called. If a collection is found, each model in the collection is serialized by calling the view's `serializeCollection` and putting into an `items` array in the resulting data. If both are found, the model is used.
  31. You can override the `serializeData` method in your own view definition, to provide custom serialization for your view's data. These serializations are then passed into the template function if one is set.
  32. @api public
  33. examples:
  34. -
  35. name: Defining custom data serialization.
  36. example: |
  37. This example sets a custom `serializeData` method to prioritize collections over models.
  38. ```js
  39. var MyItemView = Marionette.ItemView.extend({
  40. serializeData: function(){
  41. var data = {};
  42. if (this.collection) {
  43. data = { items: _.partial(this.serializeCollection, this.collection).apply(this, arguments) };
  44. }
  45. else if (this.model) {
  46. data = _.partial(this.serializeModel, this.model).apply(this, arguments);
  47. }
  48. return data;
  49. }
  50. });
  51. ```
  52. serializeModel:
  53. description: |
  54. Serialize the view's model.
  55. @api public
  56. @param {Backbone.Model} model - The model set on the ItemView to be serialized.
  57. @returns {Object} Javascript object representation of the model.
  58. examples:
  59. -
  60. name: Defining custom model serialization.
  61. example: |
  62. ```js
  63. var MyitemView = Marionette.ItemView.extend({
  64. serializeModel: function(model){
  65. return _.omit(model.attributes, 'secretData');
  66. }
  67. });
  68. ```
  69. -
  70. name: Defining custom model serialization and
  71. serializeCollection:
  72. description: |
  73. Serialize the view's collection.
  74. @api public
  75. @param {Backbone.Collection} collection - The collection set on the ItemView to be serialized.
  76. examples:
  77. -
  78. name: Defining custom collection serialization.
  79. example: |
  80. ```js
  81. var MyItemView = Marionette.ItemView.extend({
  82. serializeCollection: function(collection){
  83. return collection.map(function(model){ return _.omit(model.attributes, 'secretData'); });
  84. }
  85. });
  86. ```
  87. render:
  88. description: |
  89. 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.
  90. In general, though, you should override the `Marionette.Renderer` object to change how Marionette renders views.
  91. @api public
  92. attachElContent:
  93. description: |
  94. Attaches the content of the view. This method can be overridden to optimize rendering, or to render in a non standard way.
  95. @api public
  96. @param {String} html - The html content to set inside the view element.
  97. examples:
  98. -
  99. name: Render using `innerHTML` instead of `$el.html`
  100. example: |
  101. ```js
  102. var MyItemView = Marionette.ItemView.extend({
  103. attachElContent: function(html){
  104. this.el.innerHTML = html;
  105. return this;
  106. }
  107. });
  108. ```
  109. destroy:
  110. description: |
  111. Destroy the view.
  112. @api public
  113. @returns {Marionette.ItemView} The current view.
  114. examples:
  115. -
  116. name: Override the destroy method to trigger another event.
  117. example: |
  118. ```js
  119. var myItemView = Marionette.ItemView.extend({
  120. destroy: function(){
  121. if (this.isDestroyed) { return; }
  122. var destroyed = Marionette.View.prototype.destroy.apply(this, arguments);
  123. var args = Array.prototype.slice.call(arguments);
  124. this.triggerMethod.apply(this, ['after:destroy'].concat(args));
  125. return destroyed;
  126. }
  127. });
  128. ```