How to get JSData DS#create() to persist to data store

212 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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' });