Refactoring

This refactoring put DOM manipulation in view. And leave the app controller away from any DOM element.

The model.js file.

 1;(function($){
 2  var app = this.app = this.app || {};
 3
 4  app.model = {
 5    fetch: function(query, callback) {
 6      $.getJSON('http://api.openweathermap.org/data/2.5/weather?q=' + query + '&callback=?', function(data){
 7        callback(data);
 8      });
 9    }
10  }
11
12}).call(this, jQuery);

The view.js file.

 1;(function($){
 2  var app = this.app = this.app || {};
 3
 4  app.view = {
 5    update: function(weather) {
 6      if (weather === 'rain') {
 7        $('#rain-content').removeClass().addClass('rain');
 8      } else {
 9        $('#rain-content').removeClass().addClass('sunny');
10      }
11    }
12  }
13
14}).call(this, jQuery);

The app.js file.

 1;(function(){
 2  var app = this.app = this.app || {};
 3
 4  app.model.fetch('Macao,MO', function(data){
 5    console.log(data);
 6    var code = data.weather[0].id + ""; // force to string
 7    if (code[0] === '5') { // rainy code all start at 5
 8      app.view.update('rain');
 9    } else {
10      app.view.update('sunny');
11    }
12  });
13
14}).call(this);

What’s next? We’re going to take a look at “Refactoring again”.

overlaied image when clicked on thumbnail

Makzan | Mobile web app dev with phonegap | Table of Content