PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/comment-widget-live.backbone.js

https://gitlab.com/rockschtar/ultra-recent-comments-and-posts
JavaScript | 169 lines | 135 code | 34 blank | 0 comment | 4 complexity | b9c3aa6dda37da24a452f176acdf26d8 MD5 | raw file
  1. var brc_comment_widget_live = function(widget_id, comments) {
  2. var base = this;
  3. var CommentModel = Backbone.Model.extend({
  4. default: {comment_ID: 0, the_comment: "", prepend: false}
  5. });
  6. var CommentCollection = Backbone.Collection.extend({
  7. model: CommentModel,
  8. url: brc_comments_widgets.ajaxurl + "?action=brc_get_widget_comments&max_comment_id=0&instance_id=" + base.widget_id + "&nonce=" + brc_comments_widgets.ajaxnonce,
  9. comparator: function(item) {
  10. return item.get("comment_ID");
  11. }
  12. });
  13. var CommentView = Backbone.View.extend({
  14. tagName: "li",
  15. template: null,
  16. events: {},
  17. initialize: function() {
  18. this.template = _.template('<%= the_comment %>');
  19. this.render();
  20. },
  21. render: function() {
  22. jQuery(this.el).attr("id", "brc-comment-" + widget_id + "-" + this.model.toJSON().comment_ID);
  23. jQuery(this.el).html(this.template(this.model.toJSON())).fadeIn();
  24. return this;
  25. }
  26. });
  27. base.comment_collection = new CommentCollection();
  28. console.log(base.comment_collection);
  29. };
  30. var brc_comment_widget_live_old = function(widget_id, comments) {
  31. var that = this;
  32. this.init_comments = comments,
  33. this.widget_id = widget_id ? widget_id : null;
  34. var CommentModel = Backbone.Model.extend({
  35. default: {comment_ID: 0, the_comment: "", prepend: false}
  36. });
  37. var CommentView = Backbone.View.extend({
  38. tagName: "li",
  39. template: null,
  40. events: {},
  41. initialize: function() {
  42. this.template = _.template('<%= the_comment %>');
  43. this.render;
  44. },
  45. render: function() {
  46. jQuery(this.el).attr("id", "brc-comment-" + widget_id + "-" + this.model.toJSON().comment_ID);
  47. jQuery(this.el).html(this.template(this.model.toJSON())).fadeIn();
  48. return this;
  49. }
  50. });
  51. var CommentCollection = Backbone.Collection.extend({
  52. model: CommentModel,
  53. url: brc_comments_widgets.ajaxurl + "?action=brc_get_widget_comments&max_comment_id=0&instance_id=" + that.widget_id + "&nonce=" + brc_comments_widgets.ajaxnonce,
  54. comparator: function(item) {
  55. return item.get("comment_ID");
  56. }
  57. });
  58. var CommentsView = Backbone.View.extend({
  59. tagName: "ul",
  60. events: {},
  61. initialize: function() {
  62. console.log(this);
  63. this.collection.bind('add', this.addItemHandler);
  64. this.collection.bind("remove", this.removeItemHandler);
  65. },
  66. load: function() {
  67. this.collection.fetch({
  68. add: true,
  69. success: this.loadCompleteHandler(this),
  70. error: this.errorHandler
  71. });
  72. },
  73. addItemHandler: function(model, options) {
  74. var comment_view = new CommentView({model: model});
  75. comment_view.render();
  76. if (true === model.get("prepend")) {
  77. jQuery(this.el).prepend(comment_view.el);
  78. } else {
  79. jQuery(this.el).append(comment_view.el);
  80. }
  81. },
  82. removeItemHandler: function(model) {
  83. var comment_view = new CommentView({model: model});
  84. comment_view.render();
  85. jQuery("#brc-comment-" + widget_id + "-" + model.get("comment_ID")).remove();
  86. },
  87. errorHandler: function() {
  88. throw "Error loading Comments for Widget with ID " + that.widget_id;
  89. },
  90. render: function() {
  91. var container = jQuery('#' + that.widget_id);
  92. container.append(jQuery(this.el));
  93. return this;
  94. },
  95. refresh: function() {
  96. var refresh = this;
  97. console.log(that.comment_collection);
  98. refresh.max_comment_id = that.comment_collection.last().get("comment_ID")
  99. var refresh_interval = setInterval(function() {
  100. jQuery.ajax({
  101. type: "GET",
  102. url: brc_comments_widgets.ajaxurl,
  103. data: {
  104. action: "brc_get_widget_comments",
  105. nonce: brc_comments_widgets.ajaxnonce,
  106. max_comment_id: refresh.max_comment_id,
  107. instance_id: that.widget_id}
  108. }).done(function(response) {
  109. var comments = jQuery.parseJSON(response);
  110. for (var key in comments) {
  111. var current_comment = comments[key];
  112. that.comment_collection.remove(that.comment_collection.first());
  113. that.comment_collection.add(new CommentModel({prepend: true, comment_ID: current_comment.comment_ID, the_comment: current_comment.the_comment}));
  114. refresh.max_comment_id = current_comment.comment_ID;
  115. }
  116. that.comment_collection.sort();
  117. });
  118. }, 5000);
  119. },
  120. loadCompleteHandler: function(that) {
  121. console.log(this);
  122. that.render();
  123. that.refresh();
  124. }
  125. });
  126. var init_comments = new Array();
  127. for (var index in comments) {
  128. var current_comment = comments[index];
  129. var comment_model = new CommentModel({comment_ID: current_comment.comment_ID, the_comment: current_comment.the_comment});
  130. init_comments.push(comment_model);
  131. }
  132. that.comment_collection = new CommentCollection();
  133. that.comments_view = new CommentsView({collection: that.comment_collection});
  134. that.comments_view.load();
  135. }