微信小程序实战–集阅读与电影于一体的小程序项目(五)

21.电影页面数据绑定

movies.js


var app = getApp();
Page({
  data: {
    inTheaters: {},
    comingSoon: {},
    top250: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var baseUrl = app.globalData.g_baseUrl;
    var inTheatersUrl = baseUrl +"/v2/movie/in_theaters" + "?start=0&count=3";
    var comingSoonUrl = baseUrl + "/v2/movie/coming_soon" + "?start=0&count=3";
    var top250Url = baseUrl + "/v2/movie/top250" + "?start=0&count=3";
    this.getMovieList(inTheatersUrl, "inTheaters")
    this.getMovieList(comingSoonUrl, "comingSoon");
    this.getMovieList(top250Url, "top250");
  },

  getMovieList(url, setKey) {
    var that = this
    wx.request({
      url: url,
      data: {},
      method: 'GET',
      header: {
        'content-type': 'json' // 默认值 application/json
      },
      success: function (res) {
        console.log(res)
        that.processDoubanDate(res.data, setKey)
      }
    })
  },

  processDoubanDate: function (moviesDouban, setKey) {
    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) + "...";
      }
      var temp = {
        title: title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId: subject.id
      }
      movies.push(temp)
    }
    var readyData = {};
    readyData[setKey] = {
      movies: movies
    }
    this.setData(readyData);
      
  }
})

movies.wxml