【问题标题】:Ember model find records without server requestEmber 模型在没有服务器请求的情况下查找记录
【发布时间】:2014-12-16 10:34:43
【问题描述】:

我有一个 ember 模型类别:

export default DS.Model.extend({
  name: DS.attr('string'),
  img: DS.attr('string'),
  url: DS.attr('string'),
  cnt: DS.attr('number'),

//  parent_id: DS.belongsTo('category', {
//    inverse: 'children',
//    async: true
//  }),
  parent_id: DS.attr('string'),

//  children: DS.hasMany('category', {
//    inverse: 'parent_id',
//    async: true
//  }),
  children: DS.attr(),

  isSelected: false,
  isExpanded: false,

  hasChildren: function() {
    return this.get('children').get('length') > 0;
  }.property('children').cacheable(),

  isLeaf: function() {
    return this.get('children').get('length') == 0;
  }.property('children').cacheable()
});

在我的索引路线中,我有:

export default Ember.Route.extend({
  model: function() {
    var store = this.store;
    return Ember.ArrayProxy.create({
      categories: store.find('category'),
      menuTopCategories: store.find('category', { parent_id: 1 })
    });
  }
});

我正在使用 RESTAdapter,因此 store.find 将向服务器发送两个请求:categoriescategories?parent_id=1。 我只想有第一个请求,然后过滤类别。我试过store.all - 因为我看到它重用了已经获取的数据,但我无法应用过滤器。

【问题讨论】:

    标签: ember.js ember-data ember-router ember-model


    【解决方案1】:

    我已经重写了 menuTopCategories,但没有看到新的请求:

    menuTopCategories: store.filter('category', function(category) {
       return category.get('parent_id') === "1";
    })
    

    我现在的问题是在不硬编码 parent_id 的情况下获取根类别(第一个)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      相关资源
      最近更新 更多