Table of Content
Crafting our Rain-or-Not app
This is our root page.
The very basic yet working logic.
1;(function($){ 2 $.getJSON('http://api.openweathermap.org/data/2.5/weather?q=Macao,MO&callback=?', function(data){ 3 console.log(data); 4 var code = data.weather[0].id + ""; // force to string 5 if (code[0] === '5') { // rainy code all start at 5 6 $('#rain-content').removeClass().addClass('rain'); 7 } else { 8 $('#rain-content').removeClass().addClass('sunny'); 9 } 10 }); 11}).call(this, jQuery);
Note: the rainy code information is from the API documentation.
And the style.
1.sunny { 2 background: #ade1f9 url(../images/sunny.png) no-repeat center center; 3} 4.rain { 5 background: #ade1f9 url(../images/rain.png) no-repeat center center; 6} 7.loading { 8 background: #ade1f9 url(../images/loading.png) no-repeat center center; 9} 10 11#root { 12 background: #ade1f9; 13} 14 15#rain-content { 16 height: 400px; 17}
What’s next? We’re going to take a look at “Refactoring”.