[英]Meteor Iron:Router data not loading
I am trying to get some json data and populate a select drop down with the dates to choose results from. 我正在尝试获取一些json数据,并在选择下拉列表中填充要从中选择结果的日期。 If I load the page directly at localhost:3000/results the select drop down is populated and I can select a date to receive the correct alert so in some regard, this code works. 如果直接在localhost:3000 / results加载页面,则会填充select下拉列表,并且我可以选择一个日期来接收正确的警报,因此在某些方面,此代码有效。 But I am using iron:router and when I click on the RESULTS navigation link to render the template inside of my layout, the data isn't loaded. 但是,我使用的是iron:router,当我单击“结果”导航链接以在布局内呈现模板时,未加载数据。 What am I doing wrong? 我究竟做错了什么?
RESULTS.JS______________________________________________________________________ RESULTS.JS ______________________________________________________________________
Meteor.http.call("GET", "http://data.ny.gov/resource/d6yy-54nr.json", function (err, result){
var my_json = JSON.parse(result.content);
console.log(my_json);
var html = "<option value='' disabled default>Select a date</option>";
var showData = my_json;
//iterate over each lottery drawing and add it to the select.
//The date will be displayed, the index of the array element will be the value.
showData.forEach(function(element, index){
var date = new Date(element.draw_date);
html += "<option value='"+index+"'>"+ (parseInt(date.getMonth())+1) + "/" + date.getDate() + "/" + date.getFullYear()+ "</option>";
});
//insert the option into the select.
document.getElementById("selectDate").insertAdjacentHTML("beforeend", html);
//add an onchange event handler to the select.
document.getElementById("selectDate").addEventListener("change", displayWinningNumbers, false);
function displayWinningNumbers(e)
{
//when a option is selected, test the value. If 0 or higher return the array entry with the winning numbers.
if(e.target.value >= 0)
{
alert(showData[e.target.value].winning_numbers);
}
}
});
Solved this by doing the following: 通过执行以下操作解决此问题:
RESULTS.JS____________________________________________________ RESULTS.JS ____________________________________________________
Template.results.helpers({
results: function () {
return Session.get('lottery-results');
},
winning: function() {
return Session.get('winning')
}
});
Template.results.rendered = function(){
Session.set('lottery-results', []);
HTTP.get("http://data.ny.gov/resource/d6yy-54nr.json", function (err, result) {
Session.set('lottery-results', JSON.parse(result.content) );
});
};
Template.results.events({
'change #date': function (e, tmpl) {
Session.set('winning', $('#date option:selected').data('winning'))
}
});
Template.results.destroyed = function(){
Session.set('winning', []);
};
RESULTS.HTML____________________________________________________ RESULTS.HTML ____________________________________________________
<template name="results">
<select id="date">
<option>Select a date</option>
{{#each results}}
<option data-winning="{{ winning_numbers }}">{{dateFormat draw_date format="MM/DD/YYYY"}}</option>
{{/each}}
</select>
<h1>{{ winning }}</h1>
</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.