Filtruj rekordy podrzędne (skojarzenie hasMany) z Ember.js
Czy istnieje możliwość filtrowaniahasMany
rekordy z rekordu modelu? Chcę uzyskać aktywne projekty pogrupowane przez klienta.
Model klienta
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 })
});
Model projektu
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 })
});
Trasa projektu
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);
}
});
AktualizacjaPróbowałem czegoś takiego, ale to nie działa. Myślę, że jest to problem spowodowany żądaniami asynchronicznymi. Ale czy wskazuje to właściwy kierunek?
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);
}
});