vendredi 29 juillet 2016

Ember Data for Non Standard Rest Calls

Very new to ember sorry if any dumbness, I was trying to create a custom adapter and serializer in ember, by extending DS.RESTadapter,and Rest serializer , I was successful in getting the response and massage the data to suit the model needs, but I was not able to get the model values in the template using model property, however i got a way to get the values , request you to kindly help me with the right way

Model

App.Weather = DS.Model.extend({
    name   : DS.attr('string'),
    temperature  : DS.attr('string'),
    humidity : DS.attr('string'),
    description : DS.attr('string'),
});

Adapter

App.ApplicationAdapter = DS.RESTAdapter.extend({

    buildURL: function(item) {
        return "http://website/weather?q=" + item['q']+ "&appid="+item['appid'];
    },

    findQuery: function(store, type, query) {
        return this.ajax(this.buildURL(query), 'GET');
    }
});

Serializer

App.ApplicationSerializer = DS.RESTSerializer.extend({

    extractArray: function(store, type, payload) {
        var weathers = [{
            id: 1 // Id hard coded
        }];


        weathers[0]["name"]=payload["name"];
        weathers[0]["temperature"]=payload["main"]["temp"];
        weathers[0]["humidity"]=payload["main"]["humidity"];
        weathers[0]["description"]=payload["weather"][0]["description"];
        weathers[0]["type"]=payload["weather"][0]["main"];

        payload = { weathers: weathers };

        return this._super(store, type, payload);
    }
});

Route

App.WeatherIndexRoute = Ember.Route.extend({
    model: function(params) { 
        //debugger;
       return this.store.find('weather',{q:"London", `enter code here`appid:"b552aef423b1cf586b62d1ab1d4ef216"});

    },
    renderTemplate: function() {
        this.render('default');
    },
    setupController: function(controller, model) {

        **controller.set('model', model.content[0]);**
    }
})

As Above I am getting all the values under model.Content Array.

Not understanding y i would have to do model.content instead of just model. and why would it nest itself this way.

I am using a older version of ember ember-1.1.2.js




Aucun commentaire:

Enregistrer un commentaire