【问题标题】:Ember Data Store Find returns empty content on second callEmber Data Store Find 在第二次调用时返回空内容
【发布时间】:2014-09-18 09:10:58
【问题描述】:

我有一个带有 Fixtures 数据集的模型,因此这里不涉及后端。对于模型,我有 22 条数据记录。当我第一次在我的 IndexRoute 中查询它时,会返回所有 22 条数据记录。这里没问题。

当我离开路由,稍后再回来时,我的 IndexRoute 的模型钩子再次被调用,但这一次相同的查询没有返回数据。

我的模型钩子看起来像:

model: function () {
    var placeId = 0;

    console.log('Index Route: Model Hook');
    console.log('Getting hints for place ' + placeId);

    this.get('store').find('hint', { place: placeId })
        .then(
            function (hints) {
                console.log('Found hints', hints.get('content'));
            }
        );

    return this.get('store').find('hint', { place: placeId });
}

如您所见,出于演示目的,我总是查询地点 id 为零的提示。如前所述,它第一次返回数据(我可以在 Chrome Ember Inspector 中看到数据),但第二次进入此路由时不会返回数据(我知道它就在那里)。

编辑: 我的提示模型看起来基本上像

App.Hint = DS.Model.extend({
    title: DS.attr('string'),
    // some basic boring attributes
    place: DS.belongsTo('place', { async: true }) // Association with my Place Model
});

App.Place = DS.Model.extend({
    title: DS.attr('string'),
    // some more attributes
    hints: DS.hasMany('hint', { async: true })
});

所以查询{place: placeId} 只是获取所有与特定地点有关联的提示。问题不在于查询不起作用 - 它在第一次触发索引路由时起作用(并且它像我期望的那样起作用)。问题是索引路由的所有后续调用以及所有其他尝试访问提示的位置都不再起作用并且总是返回一个空集。

【问题讨论】:

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


    【解决方案1】:

    我终于找到了答案。问题似乎与通过与我的场所的 belongsTo 关联找到提示记录有关。

    不管怎样,我找到了这个帖子Find record from belongsTo association in Ember.js,这就是实际解决方案的样子:

    model: function () {
        var placeId = 0;
    
        return this.store.find('place', placeId)
            .then(function (place) {
                return place.get('hints');
            })
            .then(function (hints) {
                return hints;
            });
    },
    

    【讨论】:

      【解决方案2】:

      我对@9​​87654321@ 应该做什么有点困惑,因为我不确定fixtureAdapter 是否可以模拟服务器查询(从未尝试过)。

      也就是说,如果您希望您的路线始终返回夹具数据中的所有“提示”,您需要做的就是:

      return this.store.find('hint');
      

      注意:你只需要调用一次。

      如果还是不行的话。尝试发布您的夹具数据和适配器的样子。

      【讨论】:

      • 我更新了我的问题,以便更清楚地了解我的模型、它们的关联以及我想要实现的目标。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 2020-10-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多