Ember算得上等于不与ember-mirage合作

时间:2016-05-31 09:39:52

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

我试图将产品分为两类,零食和分享,但我的computed.equal并不工作。我目前没有连接数据库,因此我使用ember-mirage伪造数据。如果我删除if语句,产品会在页面上显示,但出于某种原因,我在添加if语句时不会这样做。提前谢谢。

products.hbs

   <div class='container'>
        {{#each model as |product|}}
            {{#if product.isSnack}}
                <div class='col-md-4'>
                    <img src="{{product.image}}">
                    <h3>{{product.name}}</h3>
                    <p>{{product.description}}</p>
                    <h4>£{{product.price}}</h4>
                    <button type='button' class='btn btn-xs'>ADD TO BASKET</button>
                </div>
            {{/if}}
        {{/each}}
    </div>

模型/ product.js

export default Model.extend({

name: attr('string'),
description: attr('string'),
typeOf: attr('string'),
price: attr('number'),
image: attr('string'),

isSnack: Ember.computed.equal('typeOf', 'snack'),

isShare: Ember.computed.equal('typeOf', 'share')

});

蜃/ config.js

this.get('/products', function() {
  return { 
    data: [{
      type: 'products',
      id:1,
      attributes: {
        name: "Mediterranean Snack Pop's",
        typeOf: 'snack',
        description: '',
        price: 0.80,
        image: ''
      }
    }, {
      type: 'products',
      id:2,
      attributes: {
        name: "Spicy Snack Pop's",
        typeOf: 'share',
        description: '',
        price: 0.80,
        image: ''
      }
    }
  }]
 };
});

1 个答案:

答案 0 :(得分:1)

在你的海市蜃楼响应中,你应该使用dasherized值,即 { type: 'products', id:2, attributes: { name: "Spicy Snack Pop's", 'type-of': 'share', description: '', price: 0.80, image: '' }

相关问题