Welcome Guest | Login

ajax load for yahoo-weather

Hey, has anyone built an ajax loader for yahoo-weather so that it does not crash a site it it cant connect to yahoo?

2008-07-13 03:41 PM

Hi there - I haven't built such a site - but what error is showing up in your logs when it crashes and what is the relevant code.  We can help you out as best as possible with that info.  Cheers, ~William

2008-07-13 05:40 PM

Thanks William, that would be great!

I think what happens (i wasn't able to recreate it just now) is that i get a nil object error if yahoo-weather can not connect through the internet to yahoo.

This could probably be refactored into a helper or controller, but i ust wanted to put it in to test it out.

i think its a nil error for response.

<%  @client = YahooWeather::Client.new
response = @client.lookup_location('92373') %>

<div id="header">
<div id="banner">
 <h1 id="logo"><%= link_to 'AboutRedlands.Com', pages_path %></h1>
</div>
<div id="weather">
 <%= image_tag response.image_url %>
 <div id="weatherText">
  <%=h response.condition.temp %>° <%=h response.units.temperature %><br/>
  <%= link_to "forecast", response.page_url %>
  </div>
 <div class="clearer"></div>
</div>
<div class="clearer"></div>
</div>
maybe something that fires on page load, then tests if yahoo can connect, if so, display the weather div in the header, if not, "weather unavailable"? Just wasn't sure how to do that asynchronously.

2008-07-13 07:50 PM

i did a simple fix by just adding rescues to response. I turned off my airport and recreated the error.

But with the rescues, it just posts "weather unavailable".

maybe something like this would be better?

Event.observe(window, 'load', funtion() {
   new Ajax.Request('/load_weather');

should /load_weather be a rjs.js.erb?

2008-07-13 10:29 PM

Actually yes, good call.  You could do it either way but the rjs should work fine.  Let us know if you need further help (the log should say exactly which line is throwing the error).  Cheers, ~William

2008-07-14 12:34 AM

i guess i can keep the rescues, and if time permits, do the ajax later.

so would the event.observe just go in script tags on the header

then load_weather.rjs.erb

do something like

if response.something to test if it connects

then

page.insert_html :bottom, :weather, :partial => 'layouts/weather'

else

page.insert_html :bottom, :weather, "Weather Unavailable"

how could i test if response has data?

if response.data.any?

2008-07-14 02:46 PM

It would be

if response.data.blank?  

Sounds like you're thinking about this correctly.  Let me know how it goes/went. ~William

2008-07-15 10:16 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!