Filtre os registros-filhos (hasMany associação) com Ember.js
Existe alguma possibilidade de filtrar ohasMany
registros de um registro de modelo? Eu quero obter os projetos ativos, agrupados pelo cliente.
Modelo do cliente
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 })
});
Modelo de projeto
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 })
});
Rota do projeto
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);
}
});
AtualizarEu tentei algo assim, mas não funciona. Eu acho que isso é um problema causado por solicitações assíncronas. Mas isso aponta na direção certa?
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);
}
});