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

/southpark/application/models/TweetEntry.js

https://github.com/sigmonky/LivingRoom
JavaScript | 122 lines | 32 code | 18 blank | 72 comment | 1 complexity | d413d29c415505ba42366416ab305578 MD5 | raw file
  1. /*https://async.fi/2011/07/backbone-js-automagic-syncing-of-collections-and-models/ */
  2. /* Twitter Message */
  3. TweetEntry = Backbone.Model.extend({
  4. defaults: {
  5. author: '',
  6. image_url: '',
  7. time: '',
  8. text:''
  9. }
  10. });
  11. // TweetCollection = Backbone.Collection.extend({
  12. // model: TweetEntry,
  13. // value: null,
  14. // // url: function(){ return "http://tweetriver.com/camilarc/south-park-test.json?callback=?"}
  15. // // url: function(){ return "http://localhost/LivingRoom/southpark/service/south-park-test.json"}
  16. // url: function(){ return "service/tweet-river-redirect.php"}
  17. //
  18. // });
  19. TweetCollection = Backbone.Collection.extend({
  20. model: TweetEntry,
  21. value: null,
  22. comparator: function(TweetEntry){
  23. return -TweetEntry.get("created_at");
  24. },
  25. //url: function(){ return "service/tweet-river-redirect.php"},
  26. sync: function(method, collection, success, error) {
  27. var requestData={};
  28. var last_id = ''
  29. if(collection.length>0) {
  30. requestData.since_id=collection.last().get('order_id');
  31. console.log('tweetCollection sync collection.last.id '+collection.last().get('order_id') );
  32. last_id = collection.last().get('id');
  33. }
  34. var params = {
  35. url: "service/tweet-river-redirect.php",
  36. type: "GET",
  37. data: requestData,
  38. success: success,
  39. error: error
  40. };
  41. $.ajax(params);
  42. }
  43. });
  44. // TweetCollection2 = Backbone.Model.extend({
  45. // url: 'http://localhost/LivingRoom/southpark/service/south-park-test.json',
  46. // defaults: {
  47. // models: new TweetCollection()
  48. // },
  49. // parse: function(data) {
  50. // var attrs = data && data.objects && ( _.isArray( data.objects ) ? data.objects[ 0 ] : data.objects ) || data;
  51. // var model = this;
  52. // incoming_model_uris = _.map(attrs.models, function(model) {
  53. // return model.id;
  54. // });
  55. // existing_model_uris = this.get('models').map(function(model) {
  56. // return model.get('id');
  57. // });
  58. // _.each(existing_model_uris, function(uri) {
  59. // if(incoming_model_uris.indexOf(uri) == -1) {
  60. // model.get('models').remove(model.get('models').get(uri));
  61. // }
  62. // });
  63. // _.each(incoming_model_uris, function(uri) {
  64. // if(existing_model_uris.indexOf(uri) == -1) {
  65. // model.get('models').add(_.detect( attrs.models, function(model) { return model.id == uri; }));
  66. // }
  67. // });
  68. // // _.each(attrs.models, function(model) {
  69. // // if(Sha1.hash(JSON.stringify(model)) != Sha1.hash(JSON.stringify(model.get('models').get(model.resource_uri)))) {
  70. // // model.get('models').get(model.resource_uri).set(model);
  71. // // }
  72. // // });
  73. // delete attrs.models;
  74. // return attrs;
  75. // },
  76. // initialize: function() {
  77. // _.bindAll(this, 'parse');
  78. // this.fetch();
  79. // }
  80. // });
  81. /*http://stackoverflow.com/questions/5963324/polling-a-collection-with-backbone-js */
  82. /*http://stackoverflow.com/questions/5427038/backbone-js-updating-of-models-in-a-collection */
  83. /* */
  84. // models.TweetCollection = Backbone.Collection.extend({
  85. // model: models.TweetEntry
  86. // });
  87. /*var TweetCollection = Backbone.Collection.extend({
  88. model: TweetModel,
  89. sync: function(method, collection, success, error) {
  90. var requestData={};
  91. if(collection.length>0) {
  92. requestData.last_tweet_id=collection.last.id
  93. }
  94. var params = {
  95. url: "/tweet",
  96. type: "POST",
  97. data: requestData,
  98. success: success,
  99. error: error
  100. };
  101. $.ajax(params);
  102. }
  103. }
  104. */