GameCountryRank.js 1.9 KB

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