最近,我開始學習Ember-data,並且正在用非常自定義的API寫一個應用程序。 來自後端的響應格式不正確,所以我通過'normalizeResponse'方法將它標準化爲JsonApi,並且一切正常。Ember-Data:InnerObject有EmptyObjects - whats not true
問題出現在我想看到響應內容的那一刻。當我讀到Ember-data時,我瞭解到如果有數據(InnerObjects),我將能夠通過它獲取屬性。 InnerObject.get('some_property'),但它不適用於我。 如果我想'some_property'我必須寫InnerObject.data.someproperty在長路徑看起來不好。我使用Ember.debug()來查看這個路徑,並且我的瀏覽器向我顯示屬性'_data'是EmptyObject,這是不正確的。當我點擊它時,它會顯示正確內容的列表(查看附件)。 我做錯了什麼?我是否忘記了某些東西或誤解了Ember-Data? 我將不勝感激任何幫助。
IMAGES:
export default DS.Model.extend({
facebook: DS.attr(),
www: DS.attr(),
name: DS.attr(),
street: DS.attr(),
house_number: DS.attr(),
postal_code: DS.attr(),
city: DS.attr(),
province: DS.attr(),
picture: DS.attr(),
x: DS.attr(),
y: DS.attr()
});
//json api serializer
export default ApplicationSerializer.extend({
normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
return this._super(store, primaryModelClass, this._normalizeSearch(payload), id, requestType);
},
_normalizeSearch(shops) {
let data = shops.map((obj) => {
return {
type: "search",
id: obj.id_sklep,
attributes: {
facebook: obj.facebook,
www: obj.www,
name: obj.nazwa_sklep,
street: obj.adres_ulica,
house_number: obj.adres_nr_domu,
postal_code: obj.adres_kod,
city: obj.adres_miasto,
province: obj.adres_woj,
picture: obj.zdjecie_sklep,
x: obj.lat,
y: obj.lng
}
};
});
return { data: data } ;
}
});
export default Ember.Service.extend({
getShopsAndServices(pattern) {
return this.get('store').query('search', {
fraza: pattern,
cena_min: 0,
cena_max: 100,
id_kat: 1,
lat: 53,
lng: 18
});
}
}
//Controller action:
searchRequest(pattern) {
return pattern.length > this.MIN_CHARS_FOR_SEARCH ? this.get('search').getShopsAndServices(pattern).then((results) => {
let content = results.get('content').length ? results.get('content') : [];
if (content) {
let foo = content[0];
Ember.Logger.debug(foo)
Ember.Logger.debug(foo._data.name)
Ember.Logger.debug(foo.get('name'))
}
return this.set('content', results.get('content').length ? results.get('content') : []);
}) : this.set('content', []);
},