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)

Mattia
  • 209
  • 1
  • 5
  • 12
  • 1
    I think you have to reference the list id in the user as per `belongsTo` (`...{id:1,username: 'user1', list: 1}...`), and in your payload `list` has a `user` collection, but I think it should be `users` – MilkyWayJoe May 09 '13 at 20:17
  • Thanks for helping me! Here is my jsbin: http://jsbin.com/oqihom/2/edit . Here is the output: http://jsbin.com/oqihom/2 . I get "Cannot call method 'hasOwnProperty' of undefined" Error. – Mattia May 09 '13 at 20:34
  • Not sure what's up with your code. Don't have the time to look into it right now. I can take a look tonight probably – MilkyWayJoe May 09 '13 at 20:53
  • This is really fresh, but worth mentioning: http://discuss.emberjs.com/t/ember-data-test-improvements/1199/2 this will help all adapter authors! – MilkyWayJoe May 09 '13 at 21:17
  • I have updated my JSBIN: jsbin.com/oqihom/13/edit jsbin.com/oqihom/13 . Anyone can help me? I think that my problem is caused by my redefined Adapter. Should I redefine the DS.Serializer? Thanks – Mattia May 10 '13 at 10:14
  • 1
    I'm writing [one](https://github.com/MilkyWayJoe/Ember-WebAPI-Adapter) for .NET (which is far from being done, and it's based on an existing adapter that comes with a .NET template for Ember), and it does require a custom serializer mainly because or .NET's JSON format vs Ember Data JSON format. That was the only reason to customize the serializer. I may be wrong, but since you're dealing with a websocket, you'll likely have to extend the adapter only, since it's the piece that concerns requests and connections and all of that. – MilkyWayJoe May 10 '13 at 13:36

1 Answers1

0

Reading this tip, I was unable to perform the loading of json. I think it's incorrect.

So I solved in this way, using a workaround: 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);

If you are loading a single record, you can use didFindRecord instead of didFindMany. I hope things will change in next revision.

Mattia
  • 209
  • 1
  • 5
  • 12