Reputation: 2423
I have a model with a custom adapter. I've got my adapter populating the application store but when my component calls find('model')
it does another server request -- which I do not want. The docs said that if something's recorded in the store, then it will get it from cache.
SongAdapter = ApplicationAdapter.extent
data = somePayload
new Ember.RSVP.Promise (resolve, reject) =>
path = 'some/endpoint/songs'
myPOST(path, data, @, options).then (data) ->
_.each data.songs, (entity) ->
entity.id = entity.songId
store.push('song', store.normalize('song', entity))
Ember.run(null, resolve, data)
, (jqXHR) ->
jqXHR.then = null
Ember.run(null, reject, jqXHR)
When my component loads I fetch the initial songs like this:
getSongHistory: Ember.on 'didInsertElement', ->
@store.find('song').then (data) =>
@set('songHistory', data.get('content'))
I'm missing something. I thought that find()
was supposed to search the cache first, then if empty delegate to the findAll()
method in the adapter. What am I missing?
Upvotes: 0
Views: 163
Reputation: 2423
find()
with a single argument delegates to your adapters findAll()
method which will always make a request. So my solution was to first check if the store contained any records. If it didn't then call find()
. If there were records, get them using all()
-- which doesn't make a network request -- and set the data to the array in your component.
getSongHistory: Ember.on 'didInsertElement', ->
if @store.all('song').content.length is 0
@store.find('song').then (data) =>
@set('songHistory', data.get('content'))
else
@set('songHistory', @store.all('song').content)
I hope this pattern makes sense.
Upvotes: 1
Reputation: 2661
Assuming you are using Ember Data:
If you check out the code for find
here, you can see that when you call it with one argument (in your case store.find('song')
), it delegates to findAll
which in turn delegates to fetchAll
, which states:
/**
This method returns a fresh collection from the server, regardless of if there is already records
in the store or not.
@method fetchAll
@param {String or subclass of DS.Model} type
@return {Promise} promise
*/
The find
documentation is bit misleading and should probably be changed.
Upvotes: 1