ebryn / ember-model

A lightweight model library for Ember.js
MIT License
524 stars 159 forks source link

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

Open smithcommajoseph opened 10 years ago

smithcommajoseph commented 10 years ago

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.

ahacking commented 10 years ago

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.

smithcommajoseph commented 10 years ago

@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?

ahacking commented 10 years ago

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