如何观察 Ember 数据存储的变化? [英] How to observe for changes in the Ember data store?

查看:26
本文介绍了如何观察 Ember 数据存储的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个简单的 JsBin 来展示我的目标.

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

JSBIN

我有一些 Fixture 数据,其中的每个项目都有一个名为 isFavourite 的布尔属性.indexRoute 只显示所有可用数据以及显示收藏项的第二部分.每个项目还有一个绑定到这个 isFavourite 属性的类.如果是收藏夹,则会显示红色文字.

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.

还有第二个路由,称为 dashboard,它只显示商店数据的一个子集.还有一个按钮可以切换收藏夹属性.切换时,更改会反映在索引路由上的类绑定中,但 Favourites 部分只是一个中继器,仍然显示旧数据.

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.

如何为转发器建立绑定,以便根据 isFavourite 属性更改项目?

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

推荐答案

试试这个:

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

这样做的原因是 this.store.filter 返回一个实时"集合,该集合会随着情况的变化而更新.

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

在您的 JSBIN 上为我工作.

Works for me on your JSBIN.

这篇关于如何观察 Ember 数据存储的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆