Search code examples
javascriptember.jsember-data

Ember.js. Filtered model


I have a model with tasks, and i wantto get data filtered by status and show result in different lists.

so i have a construction with does't work as i want.

    tasks: Ember.computed(function(){
        var modelTasks = this.get('store').findAll('task');

        return {
            todo: modelTasks.filterBy('status', 'todo'),
            inProgress: modelTasks.filterBy('status', 'inprogress'),
            done: modelTasks.filterBy('status', 'done')
        };
    }),

I'm new, so please be tolerant.


Solution

  • Why do you need tasks computed property?. findAll returns Promise so your code is not correct. Async computed properties little tricky - read this ignite article for more info.

    I would say, data fetching should happen at the route level, so corresponding route js file model hook you can write,

    export default Ember.Route.extend({
        model() {
            return this.get('store').findAll('task').then((result) => {
                return {
                    todo: result.filterBy('status', 'todo'),
                    inProgress: result.filterBy('status', 'inprogress'),
                    done: result.filterBy('status', 'done')
                };
            });
        }
    });
    

    inside corresponding hbs file, you can access it like model.todo