js对象动态赋值

<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>
原文地址:https://www.cnblogs.com/colorful-paopao1/p/7921194.html