(JSBIN)(JSBIN output) 所以,我有以下模型,我想加载我的数据:
App.List = DS.Model.extend({
name: DS.attr('string')
});
App.User = DS.Model.extend({
username: DS.attr('string'),
users: DS.belongsTo('App.List') // is this line correct?
});
App.List.reopen({
users: DS.hasMany('App.User')
});
我有自己的自定义适配器配置:
DS.SocketAdapter.configure('App.User',
{sideloadAs: 'users'}
);
我想使用以下有效载荷来提升我的模型:
{
"list" : {"id" : 1, "name" : "Nome", "users" : [1,2] },
"users": [
{ "id":1, "username": "user1", "users": 1 },
{ "id":2, "username": "user2", "users": 1 }
]
}
从控制台我输入:
obj = ' { "list" : {"id" : 1, "name" : "Nome", "users" : [1,2] }, "users": [{ "id":1, "username": "user1", "users": 1 },{ "id":2, "username":"user2", "users" : 1}] } ';
obj = JSON.parse(obj);
DS.get('defaultStore').load( App.List, obj);
var l_len = App.List.find().get('length');
var u_len = App.User.find().get('length');
console.log( ">>>>>>> "+ l_len + "record(s) " +u_len + "record(s)");
但似乎没有加载数据。我正在从控制台测试此模型。我正在使用ember-data revision 12.我的代码出错了什么?
PS
SocketAdapter扩展了RESTAdapter。
我应该在自定义适配器中提升数据吗?如果是的话,我不知道我的代码在哪里(如果在findMany
内,findAll
重新定义。谢谢你的帮助!
答案 0 :(得分:0)
Reading this tip,我无法执行json的加载。我认为这是不正确的。
var obj = {
"lists" : [
{ "id": 1, "name" : "ListOne", "user_ids" : [1,2] },
{ "id": 2, "name" : "ListTwo", "user_ids" : [3,4] }
],
"users": [
{ "id": 1, "username": "user1", "list": 1 },
{ "id": 2, "username": "user2", "list": 1 },
{ "id": 3, "username": "user3", "list": 2 },
{ "id": 4, "username": "user4", "list": 2 }
]
};
var store = DS.get("defaultStore");
store.load(App.List, obj);
var adapter = store.adapterForType(App.List);
adapter.didFindMany(store, App.List, obj);
如果您要加载单个记录,则可以使用didFindRecord
代替didFindMany
。我希望下一次修订会有所改变。