Ember.js过滤内容并使用按钮操作

Ember.js filter content and use button action

本文关键字:按钮 操作 js 过滤 Ember      更新时间:2023-09-26

我遇到了以下问题:我有一个控制器,我想过滤模型,比如:

App.ProductsController = Ember.ArrayController.extend({
  itemController: 'product',
  filteredContent: function(query) {
    var query = this.get('query')
    var products = this.get('content').filter(function(item) {
      return true // Condition goes here
    })
    return products
  }.property('query')
})

在我看来,它运行良好:

{{#each product in filteredContent}}
  ...
  <h1>{{product.name}}</h1>
  ...
  <button {{action addToCart}}>Add to cart</button>
{{/each}}

至少是循环。操作addToCart不起作用,导致按下按钮时出现错误Nothing handled the event 'addToCart'。尽管它在ProductController中有定义。

现在有一个有趣的部分:如果我不使用过滤后的结果,而在我的视图中只使用each product in controller,那么addToCart点击就可以工作了。我想我不明白视图和控制器之间的关系,所以我很感激任何帮助。

谢谢!

使用{{#each item in items}}语法不会更改绑定范围。因此,您已绑定到product.name。如果addToCart在ProductController中,而不是在ProductsController中,则需要将操作绑定写为action addToCart target="product"