How to observe for changes in the Ember data store?

丶灬走出姿态 提交于 2019-12-20 06:37:23

问题


I created this simple JsBin to demonstrate what I'm after.

JSBIN

I have some Fixture data and each item in it has a boolean property called isFavourite. The indexRoute just displays all the available data along with a second section that displays the items that are favourites. Each item also has a class binding to this isFavourite property. If it's a favourite, it'll have red text.

There is a second route called dashboard which only displays a subset of the data from the store. There is also a button to toggle the favourite property. When this is toggled, the change is reflected in the class binding on the index route but the Favourites section, which is just a repeater still shows the old data.

How do I establish a binding for the repeater so that the items change according to the isFavourite property?


回答1:


Try this:

App.IndexController = Ember.Controller.extend({
  favourites: function() {
    return this.store.filter('documenter', function(x) {
           ^^^^^^^^^^^^^^^^^
      return x.get('isFavourite'); 
    });
  }.property('model')
})

The reason this works is that this.store.filter returns a "live" collection that is updated as things change.

Works for me on your JSBIN.



来源:https://stackoverflow.com/questions/31259400/how-to-observe-for-changes-in-the-ember-data-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!