more-movie.js页面的一些简化
来源:
慕粉3921807
2017-01-17
data: {
navigateTitle: "",
movies:[],//重要
requesUrl:"",
totalCount:0
},
processDoubanData:function(moviesDouban){
var movies = this.data.movies;//重要
for(var idx in moviesDouban.subjects){
var subject = moviesDouban.subjects[idx];
var title = subject.title;
if(title.length>=6){
title = title.substring(0,6) + "...";
}
var temp = {
stars:util.convertToStarsArray(subject.rating.stars),
title:title,
average:subject.rating.average,
coverageUrl:subject.images.large,
movieId:subject
}
movies.push(temp);//重要
}
this.setData({
totalCount:this.data.totalCount += 20
})
this.setData({
movies:movies//重要
});
wx.hideNavigationBarLoading();
wx.stopPullDownRefresh();
},
关于加载数据这里,看老师写得好复杂,我标记重要这里是可以简化代码的
6回答
-
7七月
2017-01-18
好的,谢谢
00 -
7七月
2017-01-17
concat是要将每一次下拉加载更多的数据合并在一起,这个操作怎么去掉呢?
012017-01-18 -
7七月
2017-01-17
下面是最终版的moremovie代码,可以在这上面指出哪些代码可以简化
00 -
7七月
2017-01-17
// pages/movies/more-movie/more-movie.js var app = getApp() var util = require('../../../utils/util.js') Page({ data: { movies: {}, navigateTitle: "", requestUrl: "", totalCount: 0, isEmpty: true, }, onLoad: function (options) { var category = options.category; this.data.navigateTitle = category; var dataUrl = ""; switch (category) { case "正在热映": dataUrl = app.globalData.doubanBase + "/v2/movie/in_theaters"; break; case "即将上映": dataUrl = app.globalData.doubanBase + "/v2/movie/coming_soon"; break; case "豆瓣Top250": dataUrl = app.globalData.doubanBase + "/v2/movie/top250"; break; } wx.setNavigationBarTitle({ title: this.data.navigateTitle }) this.data.requestUrl = dataUrl; util.http(dataUrl, this.processDoubanData) }, onScrollLower: function (event) { var nextUrl = this.data.requestUrl + "?start=" + this.data.totalCount + "&count=20"; util.http(nextUrl, this.processDoubanData) wx.showNavigationBarLoading() }, onPullDownRefresh: function (event) { var refreshUrl = this.data.requestUrl + "?star=0&count=20"; this.data.movies = {}; this.data.isEmpty = true; this.data.totalCount = 0; util.http(refreshUrl, this.processDoubanData); wx.showNavigationBarLoading(); }, processDoubanData: function (moviesDouban) { var movies = []; for (var idx in moviesDouban.subjects) { var subject = moviesDouban.subjects[idx]; var title = subject.title; if (title.length >= 6) { title = title.substring(0, 6) + "..."; } // [1,1,1,1,1] [1,1,1,0,0] var temp = { stars: util.convertToStarsArray(subject.rating.stars), title: title, average: subject.rating.average, coverageUrl: subject.images.large, movieId: subject.id } movies.push(temp) } var totalMovies = {} //如果要绑定新加载的数据,那么需要同旧有的数据合并在一起 if (!this.data.isEmpty) { totalMovies = this.data.movies.concat(movies); } else { totalMovies = movies; this.data.isEmpty = false; } this.setData({ movies: totalMovies }); this.data.totalCount += 20; wx.hideNavigationBarLoading(); wx.stopPullDownRefresh() }, onReady: function (event) { wx.setNavigationBarTitle({ title: this.data.navigateTitle }) }, onMovieTap: function (event) { var movieId = event.currentTarget.dataset.movieid; wx.navigateTo({ url: '../movie-detail/movie-detail?id=' + movieId }) }, })
00 -
7七月
2017-01-17
你好。。。我没看出来你怎么简化的。关于写法的问题,讲课程和自己写项目是不一样的。讲课宁愿多写一些代码,也尽量不用简化。还是谢谢你。如果能指出如何简化就最好了。
00 -
慕粉3921807
提问者
2017-01-17
var movies = [];//这里如果写成var movies = this.data.movies;下面的几行就是多余的
if (!this.data.isEmpty) {
totalMovies = this.data.movies.concat(movies);
}
else {
totalMovies = movies;
this.data.isEmpty = false;
}
00
相似问题