2013-07-01 18 views
1

我遇到了以下問題:我有一個控制器和我要篩選的模型,是這樣的:Ember.js過濾內容和使用按鈕動作

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 controlleraddToCart點擊正在工作。我想有一些關於視圖和控制器之間的關係我不明白,所以我很感激任何幫助。

謝謝!

回答

0

使用{{#each item in items}}語法does not change the binding scope。因此你必須綁定到product.name。如果addToCart位於ProductController而不是ProductsController中,則需要將行爲綁定編寫爲action addToCart target="product"

+0

那麼'{{product.addToCart}}'導致'沒有處理事件'product.addToCart',因爲它在模型上要求這個函數,而不是'ProductController',我假設。 –

+0

我確實將邏輯移入模型中,但我不得不這樣稱呼它: 'action addToCart target =「product」' 這對我很有用。 –

+0

拍攝,沒錯。我會更新我的答案,以免混淆其他人:-) –