js对象动态赋值
2000-01-01 00:00:00 by admin 455 0
获取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);
}
注:本文转载自拾图网[http://www.tshinet.com],转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。