我是靠谱客的博主 舒心小甜瓜,这篇文章主要介绍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

最后

以上就是舒心小甜瓜最近收集整理的关于js对象动态赋值的全部内容,更多相关js对象动态赋值内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部