const RankApi = require('../net/RankApi'); cc.Class({ extends: cc.Component, properties: { scrollView: cc.ScrollView, layout: cc.Layout, rankItem: cc.Prefab, meNode: cc.Node }, // LIFE-CYCLE CALLBACKS: onLoad () { this.pageIndex = 0; this.listItem = []; for(let i = 0; i < 5; i++) { let item = cc.instantiate(this.rankItem); this.layout.node.addChild(item); item.active = false; this.listItem.push(item); } if (GameGlobal.winSize.height <= 1000) { this.scrollView.vertical = true; } }, start () { }, init() { RankApi.getAllRanks((responseData) => { console.log(responseData); this.ranks = responseData.ranks; this.me = responseData.me; this._setupList(); this._setupMeData(); }, (error) => { console.log('all rank error' + error); }); }, _setupList() { let start = this.pageIndex * 5; let end = start + 5; let sortArray = this.ranks.slice(start,end); this.listItem.forEach(n => { n.active = false; }); for(let i = 0; i < sortArray.length; i++) { let item = this.listItem[i]; item.active = true; item.getComponent('GameRankItem').init(sortArray[i]); } }, _setupMeData() { this.meNode.active = true; this.meNode.getComponent('GameRankItem').init(this.me, true); }, previousPage() { if (this.pageIndex <= 0 || this.ranks == undefined || this.ranks.length == 0) { return; } this.pageIndex -= 1; this._setupList(); this.scrollView.scrollToTop(); }, nextPage() { if (this.pageIndex >= (Math.ceil(this.ranks.length / 5) - 1) || this.ranks == undefined || this.ranks.length == 0) { return; } this.pageIndex += 1; this._setupList(); this.scrollView.scrollToTop(); } // update (dt) {}, });