Skip to content

Instantly share code, notes, and snippets.

@andrewn
Created December 15, 2010 14:21
Show Gist options
  • Save andrewn/741986 to your computer and use it in GitHub Desktop.
Save andrewn/741986 to your computer and use it in GitHub Desktop.
/**
Fetch from Weather
This fetches a JSON feed from the Weather system.
*/
var weatherWidget = {
fetch: function ( locId ) {
var weatherForecastUrl = 'http://news.bbc.co.uk/weather/forecast/' + locId + '/Next3DaysJSON.json',
cachebusterStr = 'cachebuster=' + ( new Date().valueOf() ),
jsoncallback = 'jsoncallback=weatherWidget._dataLoaded',
url;
url = weatherForecastUrl + '?' + cachebusterStr + '&' + jsoncallback;
gloader.load(
['glow', '1', 'glow.net'],
{
onLoad: function () {
glow.net.loadScript(
url,
{
// MUST set this to true to stop glow adding its
// own cachebuster
useCache: true
}
);
}
}
);
},
_dataLoaded: function ( data ) {
console.log('Weather forecast loaded. Tomorrow, %o is forecast. (Full data: %o)', data.forecastContent.forecasts[0].day.weatherType, data );
}
};
// Fetch the London forecast
weatherWidget.fetch( 8 );
/**
The whole point of the initLocation function below
is to get a Weather forecast 'loc id' that you can use
to retrieve data from the Weather system.
It first checks to see if the MYLOC cookie is set and
if not, you'd set-up a form to allow a postcode input.
*/
var initLocation = function () {
console.log('locator is ready');
// Retrieve MYLOC cookie
var myLoc = locator.getSharedLocationId();
// Has MYLOC cookie set
if ( myLoc ) {
// Call this to get data about the saved location
locator.fetchFromDataSet(
// the shared location id
myLoc,
// callback when data has loaded
function(data) {
console.log( 'The user has set My Location to %o (Weather forecast locId %o)', data.name, data.id );
},
// fetch from the 'Weather' dataset
'wcw'
);
} else {
// No shared location, here's where you'd probably
// show a text input and attach event handlers....
// When you want to do a postcode search you'd
// do something like this
var userPostcodeInput = 'W12 7TP';
locator.searchByPostcode(
userPostcodeInput,
function ( result ) {
if ( result ) {
console.log('Found a postcode %o with Weather forecast locId %o. Full result obj: %o', result.site_name, result.loc, result );
} else {
console.log('No result found');
}
}
);
}
};
// Kick off everything by calling locator.migrate()
// This is to ensure an old cookie will be migrated if
// necessary. Your callback function gets executed
// when locator is ready for you.
locator.migrate( initLocation );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment