【问题标题】:Filter multiple models by attribute in ember.js Route在 ember.js 路由中按属性过滤多个模型
【发布时间】:2014-02-07 23:46:29
【问题描述】:

我的 IndexRoute 中有以下 RSVP 哈希,它可以找到所有滑块记录(我需要使用哈希,因为我需要在此页面上加载 2 个模型)。我可以在索引模板中调用sliders或this.sliders来成功地将所有的slider对象传递给一个视图组件。

但是,我需要通过页面属性“索引”过滤这些记录。当我将 filterBy 添加到 IndexRoute 时,没有返回任何记录。

如何过滤这些记录并在模板中使用生成的数组?

索引路由

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return Ember.RSVP.hash({ 
            sliders: this.store.findAll("slider"), # Adding .filterBy("page", "index") fails to load anything
            products: this.store.findAll("products")
        });
    }
});

index.html

<script type="text/x-handlebars" data-template-name="index">
   {{mainpage-slider sliders=sliders}}
</script>

【问题讨论】:

    标签: javascript jquery ember.js ember-data


    【解决方案1】:

    将过滤器添加到控制器

    App.IndexRoute = Ember.Route.extend({
      model: function() {
        return Ember.RSVP.hash({ 
          sliders: this.store.find("slider"), # Adding .filterBy("page", "index") fails to load anything
          products: this.store.find("products")
       });
      }
    });
    
    App.IndexController = Em.ObjectController.extend({
      filteredSlider: function(){
        return this.get('sliders').filterBy('page', 'index');
      }.property('sliders.@each.page')
    });
    

    【讨论】:

    • 嗨 KingPin2k 刚刚得到这个工作谢谢!我只需要将 this.get("slider") 更改为 this.get("slider") 以反映 Route 哈希中的属性。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多