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?