【问题标题】:what is the right emberjs way to switch between various filtering options?在各种过滤选项之间切换的正确 emberjs 方式是什么?
【发布时间】:2012-04-12 04:20:03
【问题描述】:

我有一个 individualStore(从 Em.ArrayController 扩展),它的任务是保存一个单独的对象数组。我的应用程序调用了几个 API,它们返回发送到商店的各个对象。将其视为我的应用程序中缓存的单个记录的数据库。

App.individualStore = App.ArrayController.create({

  allIndividuals: function () {
    return this.get('content').sort(function (a, b) {
      return (b.votes_count - a.votes_count);
    });
  }.property('@each.votes_count').cacheable(),

  aliveIndividuals: function () {
    return this.get('content').filter(function (individual) {
      return (!!individual.living);
    }).sort(function (a, b) {
      return (b.votes_count - a.votes_count);
    });
  }.property('@each.living', '@each.votes_count').cacheable(),

  deceasedIndividuals: function () {
    return this.get('content').filter(function (individual) {
      return (!individual.living);
    }).sort(function (a, b) {
      return (b.votes_count - a.votes_count);
    });
  }.property('@each.living', '@each.votes_count').cacheable()

});

我的视图有一个 `individualsBinding: 'App.individualStore.allIndividuals',它按预期完美呈现。

我想添加过滤按钮,例如Show: All | Alive | Deceased。在这里更改过滤的正确方法是什么?请记住,无论标准是什么,我都希望它始终与 individualStore 保持同步。

有人建议在运行时更改绑定,

this.bind('users', Ember.Binding.from('App.individualStore.aliveIndividuals'));

这在我前两三次单击这些按钮时有效,但随后它冻结了浏览器(有点像无限循环?)。

这对我来说也不是最好的选择。我是 ember 的新手,所以你说的任何内容都会有所帮助。提前致谢。

【问题讨论】:

    标签: binding filter ember.js


    【解决方案1】:

    我会让过滤器函数本身成为一个属性,通过更改控制器上的filterName,您会收到通知并相应地更新过滤后的内容,请参阅http://jsfiddle.net/pangratz666/ypcLq/

    App.controller = Ember.ArrayProxy.create({
        content: [],
    
        filterName: 'all',
    
        allFilter: function() {
            return true;
        },
        aliveFilter: function(individual) {
            return ( !! individual.living);
        },
        deceasedFilter: function(individual) {
            return (!individual.living);
        },
    
        filtered: function() {
            var filterName = this.get('filterName');
            var filterFunc = this.get(filterName + 'Filter');
    
            return this.filter(filterFunc).sort(function(a, b) {
                return (b.votes_count - a.votes_count);
            });
        }.property('content.@each', 'filterName').cacheable()
    
    });
    

    因此您可以稍后在您的视图中设置过滤器,该过滤器将通过App.controller.set('filterName', 'alive') 使用。

    请注意:您可以通过this.filter(filterFunc1).filter(filterFunc2) 链接过滤器,例如,您可以过滤特定年龄的所有活着的人,...

    【讨论】:

    • 感谢您的出色回答.. 这比我所做的要好得多。不过我有一个麻烦。我想将filterNamefiltered 属性保留在我的peopleController 中,这与individualStore 不同。这是因为我希望其他控制器也以类似的方式使用 individualStore。在那种情况下,我需要.property('App.individualStore.content.@each', 'App.individualStore.content.@each.living') 之类的东西。似乎 ember 不喜欢这样的观察者。它给了我一个错误说a is undefined。有没有更简洁的方法来分离关注点?
    • 太好了,我搞定了。错误是由于未定义的过滤器造成的。我已将过滤器分组到我的 individualStore.filters 命名空间中,我的排序函数在 individualStore.sorts 命名空间中,我的 peopleController 适当地连接它们!感谢@pangratz,代码更简洁了!
    • 很高兴能帮上忙!很高兴看到!
    猜你喜欢
    • 2012-10-20
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2015-08-09
    • 1970-01-01
    • 2011-09-07
    相关资源
    最近更新 更多