2

I have a backend with routes:

resources :movies do
  collection do
    get :drafts
  end
end

store.find('movie') will load from /movies, but I want to load from /movies/drafts.

There is 'suffix' option for finders, thus I realized that I need to write custom finder. Could anyone post an example?

I tried this code:

App.Movie.reopenClass(  {
  findDrafts: function(store) {
    $.getJSON('/movies/drafts', function(payload) {
      store.pushPayload('movie', payload);
    });

    return store.all('movie');
  }
})

But #pushPayload never returns an array of models, thus I use store.all, but it returns all objects in a store. I need just retrieved models by custom ajax request.

There is also store.pushMany that returns an array of models, but it expects normalized payload. What is a proper way to normalize json before passing it to pushMany?

I can't just replace buildURL in adapter, because I want to use default urls too.

UPDATE:

Possible solution:

App.Store = DS.Store.extend({
  revision: 11,

  findAllByUrl: function(type, url) {
    var self = this;
    var promise = Ember.Deferred.create();

    $.getJSON(url, function(payload) {
      var serializer = self.serializerFor(type);
      payload = serializer.extractArray(self, self.modelFor(type), payload);
      var objects = self.pushMany(type, payload);
      promise.resolve(objects);
    });

    return promise;
  }
});

var records = this.get('store').findAllByUrl('movie', '/movies/drafts');

It seems to verbose for such simple use case. Any ideas, improvements? Am I missing something?

1 Answer 1

1

If possible you should really look at moving to Ember Data 1.0 beta. Things have changed quite a lot.

In the latest version you can override the pathForType function in your Adapter.

https://github.com/emberjs/data/blob/master/TRANSITION.md#underscored-api-endpoints

That TRANSITION document is very helpful for making the update.

Sign up to request clarification or add additional context in comments.

1 Comment

I am on the edge. I can't override pathForType because I also want to load from /movies. And also from other collection actions like movies/top_rated or movies/most_viewed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.