myFocus.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (function(exports) {
  2. var App = function() {
  3. this.page = 1;
  4. this.pageSize = 10;
  5. this.$content = $('#listWrap');
  6. this.gameZone = encodeURI($.cookie('boxGameZone')) || 'dx1',
  7. this.userId = encodeURI($.cookie('boxUserId')) || '2927006252';
  8. this.url = "http://api.lolbox.duowan.com/api/v2/player/"+ this.gameZone +"/" + this.userId + '/follows_followers/';
  9. this.lastType = 'follow';
  10. this.processType = 'follow';
  11. this.currentPage = 1;
  12. this.cacheData = {};
  13. this.init();
  14. };
  15. App.prototype.init = function() {
  16. var self = this;
  17. this.getData().done(function(data) {
  18. self.cacheData = data;
  19. self.render(1);
  20. });
  21. this.bindTabSwitchEvt();
  22. };
  23. App.prototype.render = function() {
  24. if(self.processType != self.lastType) this.page = 1;//重置 page
  25. var data = this.processType == 'follow' ? this.cacheData['follows'] : this.cacheData['followers'];
  26. data = data.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
  27. data.pageType = this.processType;
  28. data.page = this.page;
  29. data.pageSize = this.pageSize;
  30. var tmpl = _.template($('#dataTmpl').html());
  31. this.$content.html(tmpl({
  32. data: data
  33. }));
  34. };
  35. App.prototype.next = function(next) {
  36. if(next == this.page) return;
  37. this.page = next;
  38. this.render();
  39. };
  40. App.prototype.pre = function(pre) {
  41. if(pre == this.page) return;
  42. this.page = pre;
  43. this.render();
  44. };
  45. App.prototype.go = function() {
  46. var goValue = $('#go').val();
  47. if(!goValue || goValue < 0) return;
  48. this.page = goValue;
  49. this.render();
  50. };
  51. App.prototype.bindTabSwitchEvt = function() {
  52. var self = this;
  53. $('.mod-tabs-trigger').on('click', 'li', function() {
  54. var $this = $(this);
  55. $this.parent().find('li').removeClass('selected');
  56. $this.addClass('selected');
  57. self.processType = $this.attr('data-type');
  58. if(self.processType == self.lastType) return;
  59. self.lastType = self.processType;
  60. self.render();
  61. });
  62. };
  63. App.prototype.getData = function() {
  64. return $.ajax({
  65. url: this.url,
  66. dataType: 'jsonp',
  67. jsonpCallback: 'callback',
  68. jsonp: 'callback'
  69. });
  70. };
  71. function getURLParam(name) {
  72. var value = location.search.match(new RegExp("[?&]" + name + "=([^&]*)(&?)", "i"));
  73. return value ? decodeURIComponent(value[1]) : value;
  74. }
  75. exports.App = App;
  76. })(window);