简体   繁体   中英

Ember-Data - Filter Objects of a hasMany relationship

Ember 1.7 Ember-Data 1.0.9 Beta Fixture-Adapter Yo Ember-generator

I got a user model and a week model. Each user has x weeks and a week has 1 user. (It's a duty roster app).

I have a week overview which shows all users an the current selected calendar week of each user. So i have to select only the selected week of each user. If he doesn't have a calendar week yet, i have to create an empty one.

The Question is: how can I filter the relation in the router, model or in the controller so i get only the week which is currently needed? I think the problem is, that i don't use the filter or filterBy function the correct way.

User model

BuchixDienstplan.User = DS.Model.extend({
    name: DS.attr('string'),
    weeks: DS.hasMany('week', {async:true}),
    phone: DS.attr('string'),
    handy: DS.attr('string'),
    mail: DS.attr('string')
});

Week model

BuchixDienstplan.Week = DS.Model.extend({
    kwyear: DS.attr('number'),
    mo: DS.attr('string'),
    di: DS.attr('string'),
    mi: DS.attr('string'),
    do: DS.attr('string'),
    fr: DS.attr('string'),
    sa: DS.attr('string'),
    so: DS.attr('string'),
    user: DS.belongsTo('user')
});

Users route

BuchixDienstplan.UsersRoute = Ember.Route.extend({
    model: function(params) {
        users = this.store.filter('user')
        return users
    }
});

In the frontend the user can select which week to show. I show a list of all users and the week for the selected configuration. For each user I only need one special week (eg calendar week 1 for the year 2014, kwyear = 12014) So how can I filter the relation of the user model?

The Answer is

Ember.computed.filterBy

http://emberjs.com/api/#method_computed_filterBy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM