How can I get viewModel object in store filter function?

extjs, filtering, store

Solution

Way too late now, but I just had the same issue, and the cleaner method to do this is by returning filterFn as a formula bind:

For your original example:

stores: {
    agreements: {
        source: 'XXX',
        filters: [{
            filterFn: '{storeFilter}'
            }]
        }
    }
},
formulas: {
   storeFilter: function(get) {
        var somevalue = get('test').somevalue;
        return function(item) {
            return item.some_field !== this.get('test').somevalue;                
        };
   }
}

Edit: When I originally wrote this I wasn't aware that Ext continually added extra filters when using setFilters rather than just replacing them all. To get around this, you need to name the filter using an id. In the above example something like this:

        filters: [{
            id: 'myVMFilterFunction',
            filterFn: '{storeFilter}'
            }]

Then it replaces the filter as expected

Problem

I defined the store and a filter. The ViewModel contains `test` object I need to filter store items by this object. ``` Ext.define('XXX.view.XXX.ViewXXXXModel', { extend: 'Ext.app.ViewModel', ``` ... ``` stores: { agreements: { source: 'XXX', filters: { filterFn: function(item) { return item.some_field !== this.get('test').somevalue; } } } } ``` I cannot access the test object of View Model from filter function?

Original source