在hasMany上升级到Ember-Data 2.14破坏了计算过滤器

时间:2018-07-02 19:09:14

标签: ember.js ember-data

我已经尝试过一次旋转,但是不幸的是,旋转总是成功的,但是我希望这个错误对某些人来说听起来很熟悉,他们可以为我指明正确的方向。

这是个玩笑: https://ember-twiddle.com/9ee6f28479449b7b1859e4c090490775?openFiles=routes.parent.js%2C

所有名称均已更改,以使我们的数据安全人员感觉更好:

基础知识: 我有一个模型“父母”与“孩子”具有异步“ hasMany”关系。

children: hasMany('child', {async: true}),

然后我有一个绑定到hasMany的计算属性:

sons: Ember.computed.filterBy('children', 'type', 'son'),

然后我别名firstObject以获取“ sons”数组中的第一项:

firstSon: Ember.computed.alias('sons.firstObject')

在父route.setupController中,创建一个新的“儿子”对象:

setupController: function(controller, model) {
    this._super(...arguments);
    var toy = this.get('store').createRecord('toy', {
      type: 'latte'
    });
    this.get('store').createRecord('child',
        {parent: model, type: 'son', toy: toy,
        name: 'Tyler'});
  }

使用Ember 2.14和Ember-Data 2.13或任何较低版本的匹配版本,此方法有效,并且我的模板可以毫无问题地引用parent.firstSon。

一旦我将Ember-Data升级到2.14,当我到达所需的路由时,parent.children拥有正确的孩子,但是sons:filterBy为空,因此sons.firstObject的别名也为空。 (通过Ember检查器验证)

提醒:我链接的旋转键确实有效,这是我的应用程序中更复杂的版本失败了。

简单的升级操作就破坏了它-没有其他代码更改发生。

我在ember-data github中看到了一些类似的问题: 这个问题:https://github.com/emberjs/data/issues/4974与此类似,但它似乎始于2.12,似乎与避免加载数据的新的“ hasMany”调用有关。我只是绑定到实际的异步hasMany

https://github.com/emberjs/ember.js/issues/16258引用了一个类似的问题,但该问题仅以Ember 3.0.0开头,而在2.18中不存在-我的问题发生在2.14.1到〜2.18之间

有什么想法可能会破坏?

1 个答案:

答案 0 :(得分:0)

看起来这是惰性hasMany创建的另一个问题。我在执行的afterModel中调用:

  afterModel: function(model) {
    var parentPromise = this._super(...arguments);
    var loadChildren = () => {
      return model.get('children').then(children => {
        this.set('children', children);
      });
    };
    return (parentPromise && parentPromise.then) ? parentPromise.then(loadChildren) : loadChildren();
  },

并将setupController更改为:

  setupController: function(controller, model) {
    this._super(controller, model);

    var hasSons = model.get('sons').length > 0;
    if(!hasSons) {
      var toy = this.get('store').createRecord('toy', {...etc});
      var source = this.get('store').createRecord('child',
        {parent: model, type: 'son', toy: toy});
      this.get('children').removeObject(son).pushObject(son);
    }
  },

现在我有了旧功能 注意:       this.get('children')。removeObject(son).pushObject(son); 被https://github.com/emberjs/data/issues/4974推荐 而且我确实需要removeObject和pushObject才能使其正常工作