1.星星评价模板 -【stars-template.wxml】
<template name="starsTemplate">
<view class="stars-container">
<view class="stars">
<block wx:for="{{stars}}" wx:for-item="i">
<image wx:if="{{i}}" src="/images/icon/star.png"></image>
<image wx:else src="/images/icon/none-star.png"></image>
</block>
</view>
<text class="star-score">{{score}}</text>
</view>
</template>
2.在【movie-template.wxml】中引用上述模板starsTemplate,data="{{stars:stars, score: average}}" 传入stars-template.wxml中渲染
<import src="../stars/stars-template.wxml" />
<template name="movieTemplate">
<view class="movie-container" catchtap="onMovieTap" data-movieId="{{movieId}}">
<image class="movie-img" src="{{coverageUrl}}"></image>
<text class="movie-title">{{title}}</text>
<template is="starsTemplate" data="{{stars:stars, score: average}}" />
</view>
</template>
3.在【movie-list-template.wxml】中引用模板movieTemplate,data="{{...movie}}"传入movie-template.wxml中渲染
<import src="../movie/movie-template.wxml" />
<template name="movieListTemplate">
<view class="movie-list-container">
<view class="inner-container">
<view class="movie-head">
<text class="slogan">{{categoryTitle}}</text>
<view catchtap="onMoreTap" class="more" data-category="{{categoryTitle}}">
<text class="more-text">更多</text>
<image class="more-img" src="/images/icon/arrow-right.png"></image>
</view>
</view>
<view class="movies-container">
<block wx:for="{{movies}}" wx:for-item="movie">
<template is="movieTemplate" data="{{...movie}}"/>
</block>
</view>
</view>
</view>
</template>
4.目标页面【movies.wxml】引入movie-grid-template.wxml中的模板movieListTemplate,并传入数据
<view class="movies-template">
<template is="movieListTemplate" data="{{...inTheaters}}" />
</view>
<view class="movies-template">
<template is="movieListTemplate" data="{{...comingSoon}}" />
</view>
<view class="movies-template">
<template is="movieListTemplate" data="{{...top250}}"/>
</view>
5.最终数据来源,movie.js
Page({
data: {
inTheaters: {},
comingSoon: {},
top250: {},
},
onLoad: function (event) {
var inTheatersUrl = app.globalData.doubanBase +
"/v2/movie/in_theaters" + "?start=0&count=3";
var comingSoonUrl = app.globalData.doubanBase +
"/v2/movie/coming_soon" + "?start=0&count=3";
var top250Url = app.globalData.doubanBase +
"/v2/movie/top250" + "?start=0&count=3";
this.getMovieListData(inTheatersUrl, "inTheaters", "正在热映");
this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映");
this.getMovieListData(top250Url, "top250", "豆瓣Top250");
},
getMovieListData: function (url, settedKey, categoryTitle) {
var that = this;
wx.request({
url: url,
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
"Content-Type": "json"
},
success: function (res) {
that.processDoubanData(res.data, settedKey, categoryTitle)
},
fail: function (error) {
// fail
console.log(error)
}
})
},
processDoubanData: function (moviesDouban, settedKey, categoryTitle) {
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 readyData = {};
readyData[settedKey] = {
categoryTitle: categoryTitle,
movies: movies
}
this.setData(readyData);
}
})