Filter child-records (hasMany association) with Ember.js

后端 未结 1 947
渐次进展
渐次进展 2021-01-01 07:50

Is there any possibility to filter the hasMany records from a model record? I want to get the active projects, grouped by the customer.

Customer

1条回答
  •  滥情空心
    2021-01-01 08:31

    I think the following could work:

    controller

    Docket.OrganizationProjectsIndexRoute = Docket.AuthenticatedRoute.extend({
      setupController: function () {
    
        var projectsController = this.controllerFor('organization.projects');
    
        this.store.find('customer').then(function(customers) {
          var promises =  customers.map(function(customer) {
    
            return Ember.RSVP.hash({
              customer: customer,
              projects: customer.get('projects').then(function(projects) {
                return projects.filter(function(project) {
                  return !project.get('archived');
                });
              });
            });             
    
          });
    
          Ember.RSVP.all(promises).then(function(filteredProjects) {
            projectsController.set('filteredProjects', filteredProjects);
          });
    
        });            
    
      }
    });
    

    template

    {{#each filtered in filteredProjects}}
      Customer {{filtered.customer}}
    {{#each project in filtered.projects}} Project {{project.name}}
    {{/each}} {{/each}}

    The trick is use Ember.RSVP.hash to group each customer by active projects.

    0 讨论(0)
提交回复
热议问题