/app/public/js/app/controller/TweetController.js

https://bitbucket.org/juanpicado/hacker-news-reader · JavaScript · 139 lines · 103 code · 14 blank · 22 comment · 7 complexity · 936f74fd8e66de0d0e9bd343d5235cde MD5 · raw file

  1. 'use strict';
  2. define(['underscore', 'backbone',
  3. 'backbone.marionette',
  4. 'app/application',
  5. 'collections/NewsCollection',
  6. 'collections/CommentsCollections',
  7. 'views/NewsItemDetailView',
  8. 'views/NewsView'], function(
  9. _,
  10. Backbone,
  11. Marionette,
  12. NewsApp,
  13. NewsCollection,
  14. CommentsCollection,
  15. NewsItemDetailView,
  16. NewsView) {
  17. var Controller = Marionette.Controller.extend({
  18. initialize : function(options) {
  19. _.bindAll(this, "listNews", "onClose");
  20. this.newsCollection = new NewsCollection();
  21. this.commentsCollection = new CommentsCollection();
  22. this.NewsCollectionView = new NewsView({
  23. collection : this.newsCollection
  24. });
  25. },
  26. onClose: function() {
  27. // put custom code here, to close this controller
  28. delete this.newsCollection;
  29. delete this.NewsCollectionView;
  30. },
  31. /**
  32. * Initialized on start, without hash
  33. * @method
  34. */
  35. listNews : function () {
  36. var that = this;
  37. // event to destro the preview.
  38. Backbone.on("preview:destroy", function () {
  39. }, this);
  40. // event triggered afer item added in the news collection
  41. this.NewsCollectionView.on("before:item:added", function(viewInstance){
  42. //console.log("sfore:item:addedt", viewInstance);
  43. });
  44. // event triggered for each item inside the collection
  45. this.listenTo(this.NewsCollectionView, "itemview:item:load-comments", function(options) {
  46. var discuss = 'https://news.ycombinator.com/item?id=',
  47. id = options.options.model.get('item_id'),
  48. // define if the news has comments
  49. hasComments = options.options.model.get('comments') === "discuss" ? false : true;
  50. if (hasComments) {
  51. // if the news has comments, we check if the commentas was previously loaded
  52. if (options.loaded) {
  53. // only display the hidden comments
  54. options.displayComments();
  55. } else {
  56. // if not exist previous comments loaded, fetch from the server
  57. that.commentsCollection.itemId = id;
  58. //
  59. var TreeComment = Backbone.Model.extend({
  60. initialize: function(){
  61. var nodes = this.get("children");
  62. if (nodes) {
  63. this.childrens = new TreeCommentCollection(nodes);
  64. this.unset("children");
  65. }
  66. }
  67. });
  68. var TreeCommentCollection = Backbone.Collection.extend({
  69. model: TreeComment
  70. });
  71. that.commentsCollection.fetch({
  72. success: function(response, xhr) {
  73. var tree = new TreeCommentCollection(response.toJSON());
  74. var treeView = new NewsItemDetailView({
  75. collection: tree
  76. });
  77. treeView.render();
  78. // append comments and
  79. options.appendComments(treeView);
  80. //
  81. options.displayComments();
  82. //
  83. options.loaded = true;
  84. },
  85. error: function (errorResponse) {
  86. console.error('There is an error with the connection', errorResponse);
  87. }
  88. });
  89. }
  90. } else {
  91. window.open(discuss + id);
  92. }
  93. });
  94. // triggered
  95. this.listenTo(this.NewsCollectionView, "itemview:item:load-preview", function(options) {
  96. var u = 'https://news.ycombinator.com/',
  97. model_url = options.options.model.get('url');
  98. if (model_url.indexOf('item') !== 0) {
  99. $("#frame").attr("src", model_url);
  100. options.displayPreview();
  101. } else {
  102. //TODO: message
  103. }
  104. });
  105. NewsApp.reader.show(this.NewsCollectionView);
  106. // fetch news form hacker news api
  107. var refresh = function() {
  108. that.newsCollection.fetch({
  109. success: function(response, xhr) {},
  110. error: function (errorResponse) {
  111. console.error('There is an error with the connection', errorResponse);
  112. }
  113. });
  114. };
  115. // fech after load
  116. refresh();
  117. // event to refresh news from another part of the app
  118. Backbone.on("news:refresh", function () {
  119. refresh();
  120. }, this);
  121. }
  122. });
  123. return Controller;
  124. }
  125. );