0

From the code below, I would expect the console value to increase every time I run the save() function. However, the number does not update. So I'm not seeing any created values injected into the store on create.

Budget = DS.defineResource('budget')

function save(){
      Budget.create(this.budgetItem, {upsert: true})
        .then( (   ) => {
          Budget.findAll().then((data)=>console.log(data.length))
        })
}

I'm using jsdata-angular.

After the first save findAll() executes it will retreive records from the server and then cache them. After that it does not make future calls to the server when performing a findAll() (this is expected behavior) however it is also not injecting the newly created values into store either.

All my configs are left unchanged. I'm using all defaults.

Meeker
  • 5,979
  • 2
  • 20
  • 38
  • sure that `this` has valid context? If not passing undefined to `create()` – charlietfl Jun 05 '16 at 21:48
  • 1
    Try passing `useFilter: true` or `bypassCache: true` to `findAll` to see newly inserted items – jdobry Jun 05 '16 at 22:02
  • thanks for checking my work, and yes, the item is not `undefined` and is actually getting created by the adapter (in this case `http` adapter is posting to a node/mongo back end). When i refresh my browser, the `findAll()` will go out and retrieve the the newly created item from the server just fine. – Meeker Jun 05 '16 at 22:04
  • @jdobry `useFilter` did the trick? I'm going to check out docs to find out why my current usage was not working but advice would be a appreciated. THANKS FOR THE QUICK ANSWERS GUYS! – Meeker Jun 05 '16 at 22:07
  • found the docs http://www.js-data.io/docs/dsfindall, i guess its pretty plain, just seems counter productive from everything you read before about it automatically updating the store. – Meeker Jun 05 '16 at 22:09
  • 1
    The store is automatically updated, but the original query result cached by `findAll` is not. JSData cannot reliability know where to insert the newly created item into a particular cached query result, though the new item _has_ been inserted into the overall store. Personally, I never use the result of `findAll` directly in my View. Instead, `findAll` is for getting stuff _into_ the store, and `filter` is for selecting records out of a store to be used in a View. – jdobry Jun 06 '16 at 16:23
  • @jdobry thanks for the advice – Meeker Jun 06 '16 at 17:18

1 Answers1

0

DS#findAll is for retrieving records from a persistence layer via an adapter and loading them into the store. By default, if you make a findAll call and then make the exact same findAll call again (with the same query argument), then instead of making a new request, JSData returns to you the original result. You can change this behavior two ways:

  • Set useFilter to true to have your findAll invocation call DS#filter and returning you the result.
  • Set bypassCache to true to force the data to be reloaded via the adapter.

As a general rule of thumb, findAll is for asynchronously loading records into the store (e.g. via HTTP adapter GET request), and filter is for synchronously selecting records out of the store when you need them (e.g. display them in your View).

For example, in one place you do:

// Post records get retrieved and loaded into the store
store.findAll('post', { status: 'published }).then(...);

Then elsewhere:

// Need to display post records in your View
$scope.posts = store.filter('post', { status: 'published' });
jdobry
  • 1,041
  • 6
  • 17