GameCountryRank.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const RankApi = require('../net/RankApi');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. scrollView: cc.ScrollView,
  6. layout: cc.Layout,
  7. rankItem: cc.Prefab,
  8. meNode: cc.Node
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. this.pageIndex = 0;
  13. this.listItem = [];
  14. for(let i = 0; i < 5; i++) {
  15. let item = cc.instantiate(this.rankItem);
  16. this.layout.node.addChild(item);
  17. item.active = false;
  18. this.listItem.push(item);
  19. }
  20. if (GameGlobal.winSize.height <= 1000) {
  21. this.scrollView.vertical = true;
  22. }
  23. },
  24. start () {
  25. },
  26. init() {
  27. RankApi.getAllRanks((responseData) => {
  28. console.log(responseData);
  29. this.ranks = responseData.ranks;
  30. this.me = responseData.me;
  31. this._setupList();
  32. this._setupMeData();
  33. }, (error) => {
  34. console.log('all rank error' + error);
  35. });
  36. },
  37. _setupList() {
  38. let start = this.pageIndex * 5;
  39. let end = start + 5;
  40. let sortArray = this.ranks.slice(start,end);
  41. this.listItem.forEach(n => {
  42. n.active = false;
  43. });
  44. for(let i = 0; i < sortArray.length; i++) {
  45. let item = this.listItem[i];
  46. item.active = true;
  47. item.getComponent('GameRankItem').init(sortArray[i]);
  48. }
  49. },
  50. _setupMeData() {
  51. this.meNode.active = true;
  52. this.meNode.getComponent('GameRankItem').init(this.me, true);
  53. },
  54. previousPage() {
  55. if (this.pageIndex <= 0 || this.ranks == undefined || this.ranks.length == 0) {
  56. return;
  57. }
  58. this.pageIndex -= 1;
  59. this._setupList();
  60. this.scrollView.scrollToTop();
  61. },
  62. nextPage() {
  63. if (this.pageIndex >= (Math.ceil(this.ranks.length / 5) - 1) || this.ranks == undefined || this.ranks.length == 0) {
  64. return;
  65. }
  66. this.pageIndex += 1;
  67. this._setupList();
  68. this.scrollView.scrollToTop();
  69. }
  70. // update (dt) {},
  71. });