Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* APPLICATION */
  2. window.Weather = Ember.Application.create();
  3.  
  4. //Weather.ApplicationAdapter = DS.FixtureAdapter.extend();
  5. Weather.LocationAdapter = DS.Adapter.extend({
  6.   host: "http://api.openweathermap.org/data/2.5/weather",
  7.   cityKey: '?q=',
  8.   latKey: '?lat=',
  9.   lonKey: '&lon=',
  10.   find: function(store, type, id) {
  11.     return Ember.$.getJSON(this.host + this.cityKey + id);
  12.   }
  13. });
  14.  
  15. var slugify = function(str) {
  16.   return str.replace(/([^\w\s\_\-\.]|(^\W+|\W+$))/g, '').replace(/[\s\-\_\.]+/g, '-');
  17. };
  18.  
  19. /*Weather.store = DS.Store.create({
  20.   adapter: Weather.LocationAdapter.create()
  21. });*/
  22.  
  23. Weather.LocationSerializer = DS.JSONSerializer.extend({
  24.   extractSingle: function(store, type, payload) {
  25.     var name = payload.name;
  26.     var lat = payload.coord.lat;
  27.     var lon = payload.coord.lon;
  28.     payload = {
  29.       name: name,
  30.       slug: slugify(name),
  31.       lat: lat,
  32.       lon: lon
  33.     };
  34.     console.log(payload);
  35.     return this._super(store, type, payload);
  36.   }
  37. });
  38.  
  39. /* MODELS */
  40. Weather.Location = DS.Model.extend({
  41.   name: DS.attr('string'),
  42.   slug: DS.attr('string'),
  43.   lat: DS.attr('number'),
  44.   lon: DS.attr('number')
  45. });
  46.  
  47. /* ROUTER */
  48. Weather.Router.map(function() {
  49.   //this.resource('location', { path: '/' });
  50.   this.resource('location', { path: '/:location_slug' });
  51. });
  52.  
  53. /*Weather.Router.reopen({
  54.   location: 'hash'
  55. });*/
  56.  
  57. Weather.LocationRoute = Ember.Route.extend({
  58.   model: function(params) {
  59.     return this.store.find('location', params.location_slug);
  60.   }
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement