PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/main.js

https://bitbucket.org/ldavismead/backbone-twitter-search
JavaScript | 191 lines | 144 code | 41 blank | 6 comment | 17 complexity | c26f1d67bd282301f0c5c32e826640c9 MD5 | raw file
  1. function dateAge(dateString) {
  2. var ms = new Date().getTime() - Date.parse(dateString);
  3. var s = ms / 1000;
  4. if (s <= 1)
  5. return "just now";
  6. else if (s <= 60)
  7. return Math.round(s) + "s ago";
  8. var m = s / 60;
  9. if (m <= 60)
  10. return Math.round(m) + "m ago";
  11. var h = m / 60;
  12. if (h <= 24)
  13. return Math.round(h) + "h ago";
  14. var d = h / 24;
  15. return Math.round(d) + "d ago";
  16. }
  17. var pithySubtitles = [
  18. "find out what people are having for lunch",
  19. "enjoy quality political discourse",
  20. "eavesdrop on people's conversations",
  21. "lern 2 talk leik a teenager",
  22. "learn effective new pickup lines",
  23. "see pictures of cats",
  24. "discover true &lt;3"
  25. ];
  26. $(function(){
  27. var Tweet = Backbone.Model.extend({
  28. // here's where we're going to add the entities
  29. parse: function(item) {
  30. console.log(["Tweet", item]);
  31. return item;
  32. }
  33. });
  34. var Tweets = Backbone.Collection.extend({
  35. model: Tweet
  36. });
  37. var SearchResult = Backbone.Model.extend({
  38. initialize: function() {
  39. this.tweets = new Tweets;
  40. this.currentClientPage = 1;
  41. this.on("add remove change destroy sync", this.update, this);
  42. },
  43. parse: function(response) {
  44. console.log("Search result:", response);
  45. // If we're still on page 1 and the query's the same, it means we must have refreshed.
  46. // So, concatenate our more complete results onto the end of the newer ones. Preserve next_page
  47. // so we don't get out-of-order when going backwards.
  48. if (response.page == 1 && this.get("page") == 1 &&
  49. response.query == this.get("query")) {
  50. response.results = response.results.concat(this.get("results"));
  51. response.next_page = this.get("next_page");
  52. }
  53. return response;
  54. },
  55. update: function() {
  56. if (this.currentClientPage <= 0) {
  57. var url = "http://search.twitter.com/search.json";
  58. // If we're already on page 1 of the server results and we need to go newer, that means we're
  59. // doing a refresh -- otherwise we're loading the previous page of results from Twitter.
  60. if (this.get("page") == 1) {
  61. this.currentClientPage = 1;
  62. url += this.get("refresh_url");
  63. }
  64. else {
  65. this.currentClientPage = Math.floor(this.get("results_per_page") / 10);
  66. url += this.get("previous_page");
  67. }
  68. console.log("querying Twitter", url);
  69. this.fetch({dataType: "jsonp", url: url});
  70. return;
  71. }
  72. var min = (this.currentClientPage - 1) * 10;
  73. var max = min + 10;
  74. var currentData = this.get("results");
  75. if (max > currentData.length) {
  76. this.currentClientPage = 1;
  77. var url = "http://search.twitter.com/search.json";
  78. url += this.get("next_page");
  79. console.log("querying Twitter", url);
  80. this.fetch({dataType: "jsonp", url: url});
  81. return;
  82. }
  83. var part = this.get("results").slice(min, max);
  84. this.tweets.reset(part);
  85. },
  86. search: function(hashtag) {
  87. var url = "http://search.twitter.com/search.json?q=";
  88. url += encodeURIComponent("#" + hashtag);
  89. url += "&rpp=100";
  90. console.log("querying Twitter", url);
  91. this.fetch({dataType: "jsonp", url: url});
  92. },
  93. older: function() {
  94. this.currentClientPage++;
  95. this.update();
  96. },
  97. newer: function() {
  98. this.currentClientPage--;
  99. this.update();
  100. }
  101. });
  102. var TweetView = Backbone.View.extend({
  103. tagName : "li",
  104. template : _.template($("#tweet_template").html()),
  105. render: function() {
  106. this.$el.html(this.template(this.model.toJSON()));
  107. return this;
  108. }
  109. });
  110. var TweetsView = Backbone.View.extend({
  111. initialize: function() {
  112. this.model.on("add remove change destroy reset sync", this.render, this);
  113. },
  114. render: function() {
  115. var container = this.$el;
  116. if (container == null)
  117. return this;
  118. container.empty();
  119. this.model.each(function(tweet){
  120. var tv = new TweetView({model: tweet});
  121. tv.render().$el.appendTo(container);
  122. });
  123. return this;
  124. }
  125. });
  126. var AppView = Backbone.View.extend({
  127. events: {
  128. "click #searchButton": "doSearch",
  129. "click .pager .older" : "older",
  130. "click .pager .newer" : "newer"
  131. },
  132. initialize: function() {
  133. this.searchInput = this.$("#hashSearch");
  134. },
  135. doSearch: function(e) {
  136. var sr = this.searchResult = new SearchResult;
  137. this.tweetView = new TweetsView({model: sr.tweets, el: this.$("#tweets")});
  138. sr.search(this.searchInput.val());
  139. },
  140. older: function(e) {
  141. this.searchResult.older();
  142. return false;
  143. },
  144. newer: function(e) {
  145. this.searchResult.newer();
  146. return false;
  147. }
  148. });
  149. var App = new AppView({el: $("#twitterSearchApp")});
  150. });