使用Ember.js过滤子记录(hasMany关联)

时间:2013-12-11 06:18:08

标签: ember.js ember-data

是否有可能从模型记录中过滤hasMany条记录?我想获得由客户分组的活跃项目。

客户模式

Docket.Customer = DS.Model.extend({
  name:        DS.attr('string'),
  initial:     DS.attr('string'),
  description: DS.attr('string'),
  number:      DS.attr('string'),
  archived:    DS.attr('boolean'),
  projects:    DS.hasMany('project',{ async: true })
});

项目模型

Docket.Project = DS.Model.extend({
  name:        DS.attr('string'),
  description: DS.attr('string'),
  number:      DS.attr('string'),
  archived:    DS.attr('boolean'),
  customer:    DS.belongsTo('customer', { async: true })
});

项目路线

Docket.OrganizationProjectsIndexRoute = Docket.AuthenticatedRoute.extend({
  setupController: function () {

    var customersWithActiveProjects = this.store.filter('customer', function(customer) {
      return customer.get('id') && GET_ONLY_ACTIVE_PROJECTS_FROM_CUSTOMER?
    });

    this.controllerFor('organization.projects').set('filteredProjects', customersWithActiveProjects);
  }
});

更新

我尝试过类似的东西,但它不起作用。我认为这是异步请求引起的问题。但它是否指向了正确的方向?

Docket.OrganizationProjectsIndexRoute = Docket.AuthenticatedRoute.extend({
  setupController: function () {

    // get customers because we group projects by customers
    var customers = this.store.filter('customer', function(customer) {
      return customer.get('id')
    });

    var sortedProjects;

    // loop through each valid customer and filter the active projects
    $.when(

      customers.forEach(function(customer){
        customer.get('projects').then(function(projects) {

          var filteredProjects = projects.filter(function(project){
            return !project.get('archived')
          });

          customer.set('projects',filteredProjects);
        });

      })

    ).then(function() {

        sortedProjects = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
          sortProperties: ["name"],
          content: customers
        });

    });

    this.controllerFor('organization.projects').set('filteredProjects', sortedProjects);

  }
});

1 个答案:

答案 0 :(得分:3)

我认为以下方法可行:

<强>控制器

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);
      });

    });            

  }
});

<强>模板

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

诀窍是使用Ember.RSVP.hash按活动项目对每个客户进行分组。