简体   繁体   English

通过ember.js路由中的属性过滤多个模型

[英]Filter multiple models by attribute in ember.js Route

I have the following RSVP hash in my IndexRoute which finds all slider records ( I need to use a hash because I need to load 2 models on this page). 我的IndexRoute中有以下RSVP哈希,可以找到所有滑块记录(我需要使用哈希,因为我需要在此页面上加载2个模型)。 I can call sliders or this.sliders in the index template to successfully pass all the slider objects to a view component. 我可以在索引模板中调用滑块或this.sliders来成功将所有滑块对象传递给视图组件。

However, I need to filter these records by the page attribute "index". 但是,我需要通过页面属性“索引”过滤这些记录。 When I add filterBy to the IndexRoute, no records are returned. 当我将filterBy添加到IndexRoute时,没有记录返回。

How can I filter these records and use the resulting array in the template? 如何过滤这些记录并在模板中使用结果数组?

IndexRoute 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 的index.html

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

add the filter to the controller 将过滤器添加到控制器

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')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM