PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/recipe-app-v2/src/infrastructure/backbone/collection-view.js

https://github.com/ifandelse/devlink2012-Introduction-to-Backbone.js
JavaScript | 87 lines | 61 code | 11 blank | 15 comment | 4 complexity | 45e5e5366b3b5ce4822667ccd468b70e MD5 | raw file
Possible License(s): MIT
  1. define([
  2. 'underscore',
  3. 'infrastructure/backbone/recipe-app-view'
  4. ], function (_, RecipeAppView) {
  5. return RecipeAppView.extend({
  6. // Constructor method for the views inheriting from CollectionView.
  7. constructor : function () {
  8. RecipeAppView.prototype.constructor.apply(this, arguments);
  9. // Object that will hold child views.
  10. this.childViews = {};
  11. // Set "reset" & "add" listeners on the collection.
  12. if(this.collection) {
  13. this.collection.on("reset", this.render, this);
  14. this.collection.on("add", this.addChild, this);
  15. this.collection.on("remove", this.removeChild, this);
  16. }
  17. _.bindAll(this, "getViewCtor", "addChild", "removeChild", "remove", "removeChildren", "renderChildAction");
  18. },
  19. // Verifies that an added model has a viewType defined.
  20. getViewCtor : function (model) {
  21. var viewType = this.options.viewType || this.viewType;
  22. if (!viewType) {
  23. throw new Error("A `viewType` must be specified");
  24. }
  25. return viewType;
  26. },
  27. // Initializes view for each model within the collection.
  28. addChild : function (model) {
  29. var idx = this.collection.indexOf(model);
  30. var ViewCtor = this.getViewCtor(model);
  31. var view = new ViewCtor({ model: model });
  32. this.childViews[view.model.cid] = view;
  33. this.renderChildAction(view, idx);
  34. },
  35. // Calls the render method on each view instance and appends
  36. // the rendered child view to the parent container.
  37. renderChildAction: function(view, index) {
  38. view.render();
  39. this.$el.append(view.$el);
  40. },
  41. // Removes a child from childViews object.
  42. // item arg is the **model** getting removed
  43. removeChild : function (model) {
  44. if (this.childViews[model.cid]) {
  45. this.childViews[model.cid].remove();
  46. delete this.childViews[model.cid];
  47. }
  48. },
  49. // Removal of a child view is delegated to the view
  50. // itself, and that is controlled by that view's model
  51. removeChildView: function(item) {
  52. this.removeChild(item.model);
  53. },
  54. // Removes children from the childViews object.
  55. removeChildren: function() {
  56. _.each(this.childViews, this.removeChildView, this);
  57. },
  58. // Removes references to child views from the DOM.
  59. remove : function () {
  60. this.removeChildren();
  61. this.undelegateEvents();
  62. this.$el.remove();
  63. return this;
  64. },
  65. // Removes existing children. Once removed, the collection
  66. // is iterated over and each model within the collection is rendered.
  67. render: function(context) {
  68. this.trigger('preRender', context);
  69. this.removeChildren();
  70. this.collection.forEach(function(item){
  71. this.addChild(item);
  72. }, this);
  73. this.trigger('rendered', context);
  74. }
  75. });
  76. });