0

所以我试图弄清楚如何将 itemController 用于我用 regExp 过滤的项目,然后将它们作为 RSVP.hash 返回。

我有一个有很多商店的产品模型。我正在根据产品名称和商店地址和名称过滤数据。由于模型是相关的,我需要将它们作为 RSVP 返回。这引入了一个问题,当我尝试对那些过滤的项目使用 itemController 时,它们没有使用我为它们设置的 itemControllers 属性并且不返回任何内容。

我的代码在下面,我怎样才能让 itemController 工作,或者我应该以更好的方式重写这个逻辑?

产品控制器

var ProductController = Ember.ArrayController.extend({
    needs: ['application' , 'product'],
    itemController: "ProductItem",
    filteredProducts: [],
    ...
    actions: {
        changeCategory: function(category) {
            var productController = this.get('controllers.product');
            productController.set('productCategory', category);
            var shopSearchTerm = this.get('shopSearchTerm');
            var productSearchTerm = this.get('productSearchTerm');
            var regExp = new RegExp(productSearchTerm,'i');
            var shopRegExp = new RegExp(shopSearchTerm,'i');

            var promises = this.get('model').map(function(product){
                if(regExp.test(product.get('name')) && product.get('category') === productController.get('productCategory')) {
                    return Ember.RSVP.hash({
                        product: product,
                        shops: product.get('shops').then(function(shops){
                            return shops.filter(function(shop){
                                if(shopRegExp.test(shop.get('name')) || shopRegExp.test(shop.get('address'))) {
                                    return true;
                                } else {
                                    return false;
                                };
                            });
                        })
                    })
                } else {
                    return Ember.RSVP.hash({
                        product: [],
                        shops: []
                    })
                }
            });
            Ember.RSVP.all(promises).then(function (filteredProducts) {
                productController.set('filteredProducts', filteredProducts);
            });
        }
    }
    ...
});

产品-view.hbs

<h2 class="product-price">{{ product.finalprice }} &euro;</h2> {{! as expected, with filtered data this is undefined }}

产品项目控制器

var ProductItemController = Ember.ObjectController.extend({
    needs: ['application'],
    finalprice: function () {
        var rebateAmount = parseFloat(this.get('controllers.application').get('rebateAmount')).toFixed(2);
        return this.get('price') - rebateAmount;
    }.property('price', 'controllers.application.rebateAmount')
});

export default ProductItemController;

产品.hbs

{{! looping through the filtered products, explicit itemController }}
{{#each filteredProducts itemController='product-item'}}
  {{#if product}}
    {{#if shops }}
      {{ log product.finalprice }} {{! undefined, this is "Object" not "Class" }}
      {{view 'product-box'}}
    {{/if}}
  {{/if}}
{{else}}
  ...
{{/each}}

...
{{! just looping through the model }}
{{#each}} 
  {{ log this.finalprice }} {{! correct value, this is "Class" not "Object" }}
  {{view 'product-box'}}
{{/each}}
4

0 回答 0