2012-03-28 2 views
4

私はindividualStore(Em.ArrayControllerから継承)を持っています。そのタスクは個々のオブジェクトの配列を保持することです。私のアプリケーションではいくつかのAPIが呼び出され、個々のオブジェクトが返されてストアに送られます。私のアプリケーションのキャッシュされた個々のレコードのデータベースと考えてください。 'App.individualStore.allIndividuals'、完全に意図したとおりにアップレンダリング:さまざまなフィルタリングオプションを切り替えるための正しいemberjsの方法は何ですか?

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を持っています。

フィルタリングボタンを追加したいとします。 Show: All | Alive | Deceased。ここでフィルタリングを変更する正しい方法は何ですか?基準が何であれ、私はindividualStoreと常に同期していきたいと思います。

誰かがこれは、これらのボタンの私の最初の二から三回のクリックで動作しますが、それはブラウザ(無限ループのようなものを?)フリーズ

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

、実行時にバインディングを変更することが示唆されました。

これは私にとって最良の選択肢ではありません。私は新生児だから、あなたが言うことは何でも助けになるだろう。前もって感謝します。

答えて

7

私はあなたが後であなたのビューで設定できるフィルタ機能自体が財産になるだろうし、コントローラ上の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('[email protected]', 'filterName').cacheable() 

}); 

を見ますフィルタはApp.controller.set('filterName', 'alive')で使用されます。ただ、ノートとして

:することができますチェーンフィルターthis.filter(filterFunc1).filter(filterFunc2)を介したので、あなたは可能性があり、例えばフィルタのための特定の年齢のすべての生きている個人、...

+0

偉大な答えてくれてありがとう。これは何よりもはるかに良い方法です私はやっていた。 しかし、私は1つのトラブルがありました。 individualStoreとは異なる 'peopleController'の中に' filterName'と 'filtered'プロパティを保持したいと思います。これは、他のコントローラにも同様の方法でindividualStoreを使用させたいからです。 その場合、 '.property( 'App.individualStore.content。@ each'、 'App.individualStore.content。@ each.living')などのようなものが必要です。emberは好きではないようですそのような観察者。それは私に 'a is undefined 'と言うエラーを与えます。懸念を分けるためのよりクリーンな方法がありますか? –

+0

偉大な、私はこれをやった。エラーは定義されていないフィルタによるものです。フィルターを私の 'individualStore.filters'名前空間にグループ化し、私の並べ替え関数を' individualStore.sorts'名前空間の中に入れました。私のpeopleControllerはそれらを適切に接続します! @pangratzのおかげでコードははるかにクリーンです! –

+0

私は助けることができてうれしいです!お目にかかれて光栄です! – pangratz

関連する問題