簡體   English   中英

流星鐵:路由器數據未加載

[英]Meteor Iron:Router data not loading

我正在嘗試獲取一些json數據,並在選擇下拉列表中填充要從中選擇結果的日期。 如果直接在localhost:3000 / results加載頁面,則會填充select下拉列表,並且我可以選擇一個日期來接收正確的警報,因此在某些方面,此代碼有效。 但是,我使用的是iron:router,當我單擊“結果”導航鏈接以在布局內呈現模板時,未加載數據。 我究竟做錯了什么?

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); 
  }
}
});

通過執行以下操作解決此問題:

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 ____________________________________________________

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM