私は ember と ember-data を使用して、サーバーから json フィードを消費しようとしています。これが私のコードです:
App = Ember.Application.create();
DS.RESTAdapter.configure(
"plurals", {
category: 'categories'
}
);
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.create({
url: 'app'
})
});
App.Router.map(function(){
this.resource('categories');
});
App.CategoriesRoute = Ember.Route.extend({
model: function() {
return App.Category.find();
}
});
var attr = DS.attr;
App.Category = DS.Model.extend({
name: attr('string')
});
これで、テスト サーバーで問題なく動作します。次の JSON を使用する
{
"categories":[
{
"name":"Beef",
"id":1
},
{
"name":"Pork",
"id":2
}
]
}
ただし、本番環境では、サーバーは次の json を提供します。
{
"success":true,
"message":"Request successful",
"total":2,
"data":[
{
"name":"Beef",
"id":1
},
{
"name":"Pork",
"id":2
}
]
}
私の人生では、シリアライザーを使用してライブjsonを消費する方法を理解することはできません。どんな助けでも大歓迎です。前もって感謝します。
アップデート:
それ以来、シリアライザーを書き込もうとしましたが、機能していないようです...
下記参照
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.create({
url: 'app',
serializer: DS.RESTSerializer.extend({
extract: function(loader, json, type, record) {
var root = 'data';
this.sideload(loader, type, json, root);
this.extractMeta(loader, type, json);
if (json[root]) {
if (record) { loader.updateId(record, json[root]); }
this.extractRecordRepresentation(loader, type, json[root]);
}
}
})
})
});
これでこのエラーが発生しますUncaught Error: assertion failed: Your server returned a hash with the key data but you have no mapping for it