1

(JSBIN) (JSBIN output) So, I have the following model and I would to load my data:

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

I have my own custom adapter configuration:

DS.SocketAdapter.configure('App.User',
    {sideloadAs: 'users'}
);

I would to boostrap my model with the following payload:

{ 
    "list" : {"id" : 1, "name" : "Nome", "users" : [1,2] }, 
    "users": [
                { "id":1, "username": "user1", "users": 1 },
                { "id":2, "username": "user2", "users": 1 }
             ] 
} 

From console I type:

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

But it seems that no data was loaded. I am testing this model from console. I am using ember-data revision 12. Where is wrong in my code?

ps SocketAdapter extends RESTAdapter. Should I boostrap my data inside my custom Adapter? If yes, I don't know where put my code (if inside findMany, findAll redefinition). Thanks for help!

(JSBIN) (JSBIN output)

4

1 回答 1

0

阅读此提示,我无法执行 json 的加载。我认为这是不正确的。

所以我以这种方式解决了,使用了一种解决方法:JSBIN output

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. 我希望在下一次修订中情况会有所改变。

于 2013-05-11T16:28:31.640 回答