<view class="movies-template">
<template is="movieListTemplate" data="{{movies}}" />
</view>
<view class="movies-template">
<template is="movieListTemplate" />
</view>
<view class="movies-template">
<template is="movieListTemplate" />
</view>
獲取movies有用的數據后,怎樣一 一對應各自的數據呢。
Page({ data:{ inTheaters: {}, comingSoon: {}, top250: {}//定義這三個結構體變量可以分別綁定在xml頁面中 } })
在js中 調用的地方可以區分三種數據
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" }, // 設置請求的 header success: function (res) { // success console.log(res) that.processDoubanData(res.data, settedkey, categoryTitle) }, fail: function () { // fail console.log("failed") } }) },
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,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 readayData = {}; readayData[settedkey] = {movies:movies}; //動態屬性 this.setData(readayData); }
<view class="container" wx:if="{{containerShow}}"> <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> </view>
