Filtering child records in Ember Data

I have not been able to filter the childrecords of objects in ArrayController .

The structure of my models is like this:

var Shop = DS.Model.extend({
    name: DS.attr('string'),
    products: DS.hasMany('product')
});

var Product = DS.Model.extend({
    name: DS.attr('string'),
    shop: DS.belongsTo('shop')
});

Shop has many products, and product belongs to a shop. I would like to filter the childrecords of each parent based on a Ember.TextField . Filtering works if I'm only filtering the parent records based on a property they have, using a regexp.

productSearchResults: function() {
    var productSearchTerm = this.get('productSearchTerm');
    var regExp = new RegExp(productSearchTerm,'i');

    Ember.Logger.log('productSearchTerm', productSearchTerm);

    var filteredResults = this.map(function(shop){
        var products = shop.get('products');
        return products.filter(function(product){
            regExp.test(product.get('name'));
        });
    });
    // all items are returned always..
    return filteredResults;
}.property('products.@each', 'productSearchTerm')

Edit

I tried to use promises here (source: Filter child-records (hasMany association) with Ember.js ), but it seems like this productSearchResults property is never accessed. I do not get any log output from here. In the template, I'm looping over filteredProducts and there is nothing there. If it's of any relevance, I'm using Ember 1.5.0 and Ember Data 1.0.0-beta.7+canary.b45e23ba .

   productSearchResults: function() {
        var _that = this;
        var productSearchTerm = this.get('productSearchTerm');
        var regExp = new RegExp(productSearchTerm,'i');

        this.store.find('shop').then(function(shops) {
            var promises = shops.map(function (shop) {
                return Ember.RSVP.hash({
                    shop: shop,
                    products: shop.get('products').then(function (products) {
                        return products.filter(function (product) {
                            return regExp.test(product.name);
                        });
                    })
                });
            });

            Ember.RSVP.all(promises).then(function (filteredProducts) {
                _that.set('filteredProducts', filteredProducts);
            });
        });
    }.property('products.@each', 'productSearchTerm')
链接地址: http://www.djcxy.com/p/65584.html

上一篇: 到另一个资源抛出未捕获的#error

下一篇: 在Ember Data中过滤子记录