/app/public/js/collections/sits.js

https://bitbucket.org/dbezrukov/photoload · JavaScript · 86 lines · 64 code · 22 blank · 0 comment · 2 complexity · 3411cb87d2b7fb80253bf19a6c7fb0b8 MD5 · raw file

  1. define([
  2. "underscore",
  3. "underscore.string",
  4. 'backbone',
  5. 'models/sit'
  6. ], function(_, _str, Backbone, Sit) {
  7. var lastQuery;
  8. var offset = 0;
  9. var somePhotosLoaded = false;
  10. return Backbone.Collection.extend({
  11. model: Sit,
  12. url: '/sits',
  13. initialize: function(models, options) {
  14. this.users = options.users;
  15. },
  16. doFetch: function(query){
  17. var self = this;
  18. function formatDate(date) {
  19. return (date.toLocaleDateString() + ' ' + date.toLocaleTimeString());
  20. }
  21. query.offset = offset;
  22. query.count = somePhotosLoaded ? 20 : 75;
  23. var query_string = $.param(query);
  24. $.ajax({
  25. type: "GET",
  26. url: self.url,
  27. data: query_string,
  28. dataType: 'JSON',
  29. success: function(data){
  30. somePhotosLoaded = true;
  31. offset += query.count;
  32. self.trigger(data.length < query.count ? 'nomore' : 'hasmore');
  33. data.forEach(function(sit) {
  34. sit.src = '/img/sit64.png',
  35. sit.src_preview = '/img/sit64.png',
  36. sit.time_string = formatDate(new Date(sit.utc * 1000));
  37. var user = self.users.get(sit.userId);
  38. sit.user_name = user ? user.get('name') : 'недоступно';
  39. sit.title = sit.name;
  40. self.add(sit);
  41. });
  42. },
  43. error: function(xhr){
  44. }
  45. });
  46. },
  47. fetch: function(query_object){
  48. var self = this;
  49. offset = 0;
  50. somePhotosLoaded = false;
  51. if (query_object) {
  52. lastQuery = query_object;
  53. }
  54. self.reset();
  55. self.doFetch(lastQuery);
  56. },
  57. fetchMore: function(){
  58. var self = this;
  59. if (lastQuery) {
  60. self.doFetch(lastQuery);
  61. }
  62. }
  63. });
  64. });