2016-07-29 36 views
0

非常新的呃抱歉,如果任何愚蠢,我试图创建一个自定义适配器和串行器在烬中,通过扩展DS.RESTadapter和Rest序列化程序,我成功地获得响应和按摩数据以适应模型需要,但我无法使用模型属性获取模板中的模型值,但是我有一种方法来获取值,请求您以正确的方式帮助我Ember非标准休息电话的数据

模型

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

适配器

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'); 
    } 
}); 

串行

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); 
    } 
}); 

路线

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]);** 
    } 
}) 

正如上面我下model.Content阵让所有的值。

不明白我不得不做模型model.content而不是模型。为什么它会以这种方式嵌套自己。

我使用烬 烬,1.1.2.js

+0

您不需要在setupController挂钩中设置模型属性。因为这将由父控制器自动设置。确保您应该调用this._super(.. arguments) – kumkanillam

+0

有关嵌套适配器的信息,请参阅:幻灯片(http://www.slideshare.net/RyanMHarrison/nest-v-flat-with-emberdata)和/或Video( https://www.youtube.com/watch?v=DjAMSjFPLW8) – rmharrison

回答

0

如果你正在努力学习灰烬的旧版本,不要使用版本,你不能找到文档。如果您不能使用版本2.x,请下载1.13。

最简单的方法,如何处理非标准REST响应是修改normalize()方法[http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalize]它应该采取任何响应,并以您的序列化程序所需的格式创建JSON。