【发布时间】:2015-09-15 09:58:07
【问题描述】:
助手在会话更改时(在同一页面上)运行多次(它们取决于会话,因为这是我选择加载哪些数据的方式)
我从 JSON (Meteor.http.get...) 获取数据 **检查服务器方法*
API 密钥存储在用户(useraccounts 包,当前按预期工作)
API 密钥端点(如果需要):https://keeky.github.io/Guild-Wars-2-API-Explorer/#v2/account Meteorpad : 6mY7c6yf8pAjACyZe/Leaderboard (还没有检查它是否在那里工作)
视频(问题):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);
【问题讨论】:
-
不是很明显,但那是很多代码。
-
是的...将其减小到“可读”大小,因为我基本上添加了流星文件夹中的所有内容
-
我没有通读您的所有代码,但您通常不应该从数据助手调用方法,而是从事件处理程序或跟踪器回调中调用方法。当你在一个 helper 中做这件事时,尤其是一个根据这个调用的副作用计算出来的 helper,这个 helper 会被运行多次,进行更多的调用,所以在最坏的情况下,它会被无限期地调用。跨度>
标签: meteor meteor-blaze