2013-12-11 58 views
1

是否可以從模型記錄中篩選hasMany記錄?我想獲得由客戶分組的活動項目。使用Ember.js篩選子記錄(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); 

    } 
}); 

回答

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到組每個CU活躍的項目。

+0

謝謝,但輸出只是「客戶客戶」。 – Slevin

+0

我會盡力在小提琴後面模擬這個問題 –

+0

我認爲問題是,包含在'filteredProjects'中的對象是承諾。 'console.log(filteredProjects)'返回:http://gopeter.de/misc/promises.png。 2的長度是正確的,因爲我的數據庫中有2個客戶 – Slevin