1
votes

I am adding a JSON API to the Ember tutorial app found here with Ember-Data. The flow of the app goes like this:

  • Load the website: App fetches todos from the API via a GET request.
  • Add a todo: App add a new todo via a POST request
  • Edit the todo that was just added: Fails because although the todo is in the database, the app doesn't know about it, because it hasn't gotten the list of todos since the todo was added.
What I'm thinking I need is to refresh the list of todos every time a new one is added. I've tried to do something like this, but it only observes a property.
1

1 Answers

0
votes

Just use a Live Record Array

Takes a type and filter function, and returns a live RecordArray that
remains up to date as new records are loaded into the store or created
locally.

The callback function takes a materialized record, and returns true
if the record should be included in the filter and false if it should
not.

The filter function is called once on all records for the type when
it is created, and then once on each newly loaded or created record.

If any of a record's properties change, or if it changes state, the
filter function will be invoked again to determine whether it should
still be in the array.

IE

this.store.all('post')

this.store.filter('post', function(post){ return post.get('body').indexOf('ember')>=0;});

Example:

http://emberjs.jsbin.com/OxIDiVU/22/edit