对使用RSVP.hash过滤的项目使用itemController属性

时间:2014-05-23 13:14:24

标签: javascript ember.js handlebars.js ember-cli

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

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

我的代码如下,如何让itemController工作,或者我应该以更好的方式重写这个逻辑?

ProductController的

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 }}

ProductItemController

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;

product.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}}

0 个答案:

没有答案