mercredi 1 février 2017

Ember Web App - Trouble displaying JSON data - Open Weather Map API

I am new to Ember app development and I am getting stuck on how I get the data displayed on a different page.

I believe in Ember terms... I have a main application, the only route I have currently is called "request-location". The "app/routes/request-location.js" has a Model() hook and there it calls to the Open Weather Map API (I tried working it into an adapter I just couldn't figure it out and since I got this response working I am posting about it).

I tried pushing the data to the store as you see from the commented out area (that was how another post showed it). The adapter I tried using was the JSONAPIAdapter but I also tried the RESTAdapter. I attempted following tutorials which used Normalizer's and Serializer's but I haven't had any luck.

I can read each piece of data in the responseJSON.name (or whatever else). I know my model only has the name currently I am just trying to get this to work first before I get the rest of the data. I just don't know how to get it to display in the request-location.hbs file. Below is my code. I appreciate all of your help in advance.

// routes/request-location.js

import Ember from 'ember';
import config from '../config/environment';

export default Ember.Route.extend({
  model() {
    var key = config.myApiKey;
    var location = "denver";
    var url = 'http://ift.tt/1jfvxJq' + location + '&appid=' + key;
    console.log('url: ' + url);
    Ember.$.getJSON(url).then((responseJSON) => {

      console.log('name: ' + responseJSON.name);

      //this.store.pushPayload(responseJSON.name); //.results);
      return responseJSON.name;
    });
  }
});
// Returned JSON Object

{
    "coord": {
        "lon": -0.13,
        "lat": 51.51
    },
    "weather": [{
        "id": 701,
        "main": "Mist",
        "description": "mist",
        "icon": "50d"
    }, {
        "id": 741,
        "main": "Fog",
        "description": "fog",
        "icon": "50d"
    }],
    "base": "stations",
    "main": {
        "temp": 279.4,
        "pressure": 1011,
        "humidity": 93,
        "temp_min": 278.15,
        "temp_max": 280.15
    },
    "visibility": 8000,
    "wind": {
        "speed": 2.6,
        "deg": 30
    },
    "clouds": {
        "all": 90
    },
    "dt": 1485766200,
    "sys": {
        "type": 1,
        "id": 5091,
        "message": 0.191,
        "country": "GB",
        "sunrise": 1485762062,
        "sunset": 1485794844
    },
    "id": 2643743,
    "name": "London",
    "cod": 200
}
<!-- /templates/request-location.hbs -->

<h2>Request-location Weather Data</h2>

<div class="container">
  <ul>
    
      <li></li>
    
  </ul>
</div>
// /models/request-location.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr()
});



Aucun commentaire:

Enregistrer un commentaire