2013-12-14 62 views
1

我正在使用Ember-Data向Ember教程应用程序here添加JSON API。该应用程序的流程如下所示:当数据添加到Ember-Data模型时,刷新Ember-Data模型

  • 加载网站:应用程序通过GET请求从API中获取待办事项。
  • 添加待办事项:应用通过POST请求添加新待办事项
  • 编辑刚添加的待办事项:失败,因为尽管待办事项在数据库中,但应用程序并不知道它,因为它没有得到自待办事项发布以来的待办事项列表。
我在想我需要的是每次添加新的时候刷新todos列表。我试过做类似 this的东西,但它只是观察属性。

回答

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

+0

试过了,仍然没有工作。也许这个问题比不同步的数据更大。 – Januzellij

+0

你如何添加一个新的记录,原始的ajax调用?或者你正在创建一个新的记录? (){ – Kingpin2k

+0

'createTodo:function(){ var title = this.get('newTitle'); (!title.trim()){return; } 变种待办事项= this.store.createRecord( '待办事项',{ 标题:标题, isCompleted:假 }); todo.save(); }' – Januzellij