当会话发生变化时,帮助者会多次(在同一页面上)运行(他们依赖于会话,因为我选择加载哪些数据)
我从JSON获取数据(Meteor.http.get ...)**检查服务器方法*
Api密钥存储在用户(useraccounts包,目前按预期工作)
API Key端点(如果需要):https://keeky.github.io/Guild-Wars-2-API-Explorer/#v2/account Meteorpad:6mY7c6yf8pAjACyZe /排行榜(没有检查它是否在那里工作)
视频(问题):http://webm.host/b8c71/vid.webm
客户端
myprofile.html
<template name="myprofile">
<ul class="nav nav-tabs nav-justified" style="margin-bottom: 15px;">
{{#each characterlist}}
<li><a name="{{this}}" class="charactername" href="#">{{this}}</a></li>
{{/each}}
</ul>
<div class="container">
<div class="well">
<h1 class="text-center">{{character_selected_name}}</h1>
<h6 class="text-center">Level {{character_selected_data.level}} {{character_selected_data.profession}}</h6>
<hr>
</div>
<div class="well">
<h2 class="text-center">Bags</h2>
<h5 class="text-center">
{{#each character_selected_data.bags}}
{{id}}
{{/each}}
</h5>
<hr>
</div>
</div>
<div class="footer-myprofile">
<div class="row whitetext text-center boldtext">
<div class="col-md-4">
{{basicinfo.name}}
</div>
<div class="col-md-4">
{{basicinfo.world}}
</div>
<div class="col-md-4">
{{#each basicinfo.guilds }}
{{this}}
{{/each}}
</div>
</div>
</div>
</template>
myprofile_helpers.js
Template.myprofile.helpers({
"basicinfo" : function(){
Meteor.call("getmyprofilebasic", function(error,result){
result.world = worlddata.findOne({"worldid":result.world}).name;
Session.set("basicinfo",result);
});
return Session.get("basicinfo")
},
"characterlist" : function(){
Meteor.call("getmyprofilecharacters", function(error,result){
result = result.sort();
Session.set("characterlist",result);
Session.set("character_selected",result[0])
console.log(result);
});
return Session.get("characterlist")
},
"character_selected_name": function(){
return Session.get("character_selected")
},
"character_selected_data": function(){
Meteor.call("getmyprofilecharacterdata",Session.get("character_selected"), function(error,result){
console.log(result);
Session.set("character_selected_data",result);
});
return Session.get("character_selected_data")
}
});
myprofile_events.js
Template.myprofile.events({
"click .charactername" : function(event,template){
Session.set("character_selected",this.toString())
event.preventDefault();
return false
}
});
服务器端
服务器方法(myprofile)
Meteor.users.deny({
update: function() {
return true;
}
});
if (Meteor.isServer){
Meteor.methods({
"getmyprofilebasic" : function(){
this.unblock();
var userid = this.userId;
console.log(userid);
var apikey = Meteor.users.findOne({_id:userid}).profile.apikey;
var url="https://api.guildwars2.com/v2/account?access_token=" + apikey;
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
var respJson = JSON.parse(result.content);
return respJson;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
},
"getmyprofilecharacters" : function(){
this.unblock();
var userid = this.userId;
var apikey = Meteor.users.findOne({_id:userid}).profile.apikey;
var url="https://api.guildwars2.com/v2/characters?access_token=" + apikey;
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
var respJson = JSON.parse(result.content);
return respJson;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
},
"getmyprofilecharacterdata" : function(name){
this.unblock();
var userid = this.userId;
var apikey = Meteor.users.findOne({_id:userid}).profile.apikey;
var url="https://api.guildwars2.com/v2/characters?access_token=" + apikey +"&ids=" +name;
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
var respJson = JSON.parse(result.content);
return respJson[0];
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
}
});
}
流星方法(getitemicon)
Meteor.methods({
"getitemicon" : function(id){
this.unblock();
var url="https://api.guildwars2.com/v2/items?ids=" + id;
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
var respJson = JSON.parse(result.content);
return respJson[0].icon;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
}
});
Meteor.call("getitemicon",8932);
答案 0 :(得分:2)
确实有很多代码,但模式最终会显示出来:您的配置文件页面需要需要对 guildwars2 api进行多次API调用的信息。每个页面元素都使用帮助器和定义一个会话变量,每个元素都需要对服务器进行异步调用,这反过来又会调用 guildwars2。 Ugh。这不会顺利。
这是一种替代方法,可以让您获得更好的性能和可读性。
Meteor.call
Template.myprofile.onCreated()
GuildWars2Data
组织将数据转换为在UI中运行的结构。该集合也应该键入用户,因为我怀疑你的GuildWars2数据与Meteor用户是1:1。这个集合基本上是一个缓存。GuildWars2Data
并且服务器正在发布与登录用户相关的文档GuildWards2Data
文档{{characterName}}
。如果你做得好第二步,你可能甚至不需要任何帮助器来设置数据上下文。现在你应该能够抛弃所有这些会话变量,因为你有一个集合可以引用。
现在我想知道你为什么要首先通过你的服务器中介所有这些电话。如果每个用户都有自己的GuildWars2 api密钥,那么获取自己数据的代码就可以在客户端上运行。这样可以节省您在服务器上的负担,并为您的用户提供更快的速度。您需要小心发布用户&#39; API密钥仅适用于拥有它们的用户。