ember-model icon indicating copy to clipboard operation
ember-model copied to clipboard

Working with ember-model adapters that only deal with sideloaded data

Open smithcommajoseph opened this issue 11 years ago • 3 comments

I have this request for knowledge documented here, but it has one unanswered for a few days, so I'm posting here in hopes someone will have an answer. http://stackoverflow.com/questions/25452380/working-with-ember-model-adapters-that-only-deal-with-sideloaded-data.

Ember-model's documentation has an example like the following

// Embedded Relationship Example

postJson = {
  id: 99,
  title: 'Post Title',
  body: 'Post Body',
  comments: [
    {
      id: 1,
      body: 'comment body one',
    },
    {
      id: 2,
      body: 'comment body two'
    }
  ]
};

App.Post = Ember.Model.extend({
  id: Ember.attr(),
  title: Ember.attr(),
  body: Ember.attr(),
  comments: Ember.hasMany('App.Comment', {key: 'comments', embedded: true})
});

App.Comment = Ember.Model.extend({
  id: Ember.attr(),
  body: Ember.attr()
});

If I wanted to implement an adapter so I could findAll comments, via App.Comment.findAll(), what would that look like?

Once this process is understood, I would be happy to submit a PR with an update to the README to include this process as I think it could be helpful to others.

smithcommajoseph avatar Aug 25 '14 15:08 smithcommajoseph

Calling App.Comment.find() will find all currently loaded comments care of the posts they were loaded from. Each model class maintains an internal identity map/cache of all records of that type as well as a sideload data cache for any data that has not yet been materialised into records.

If you want to fetch comments from the server, then by configuring the appropriate REST endpoint as you have done for say Post on your Comment model, you should just be able to fetch them and no special adapter is required.

ahacking avatar Aug 27 '14 02:08 ahacking

@ahacking what you describe, using App.Comment.find() is what I would expect (which appears to call findAll if no arguments are passed), however I consistently get a error 'Ember.Adapter must implement findAll'.

This is why I ask, what would the adapter need to look like to implement this?

smithcommajoseph avatar Sep 05 '14 20:09 smithcommajoseph

What adapter have you configured on your comment model? Are you using the built in REST adapter or something else?

ahacking avatar Sep 06 '14 00:09 ahacking