添加数据时刷新Ember-Data模型

时间:2013-12-14 20:42:39

标签: ember.js ember-data

我正在使用Ember-Data向找到here的Ember教程应用添加JSON API。应用程序的流程如下:

  • 加载网站:应用程序通过GET请求从API获取待办事项。
  • 添加待办事项:应用程序添加新的待办事项通过POST请求
  • 编辑刚刚添加的待办事项:失败,因为虽然todo在数据库中,但应用程序并不知道它,因为它没有得到todos列表,因为它todo被添加了。
我想我需要的是每次添加一个新的时候刷新待办事项列表。我试图做this之类的事情,但它只观察一个属性。

1 个答案:

答案 0 :(得分:0)

只需使用实时记录数组

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

示例:

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