Ember数据首先从存储获取记录,而不是使用param从API获取记录

Ember Data Get Record From Store First Rather than API with param

本文关键字:获取 记录 param API 存储 数据 Ember      更新时间:2023-09-26

我正试图在ember-cli应用程序中基于slug而不是路由中的id加载一些数据。我已经创建了一个路由,slug参数被正确地传递到模型挂钩,并且我已经在存储中有了记录数据,因为一些数据在初始化时会被引导到应用程序中。然而,商店总是关注api路由,而不仅仅是获取本地版本。我该如何防止这种情况发生?

router.js

this.resource('vendor', {path: '/:slug'}, function () {
      this.resource('category', {path: '/:vendor_id/categories/:category_id'});
      this.resource('product', {path: '/:vendor_id/products/:product_id'});
    });

routes/vendor.js

export default Ember.Route.extend({
    model: function(params) {
        return this.store.query('vendor', {slug: params.slug});
    }
});

然后在控制台中,我可以看到api调用被触发到

http://localhost:4200/api/vendors?slug={slug from params} 404 (Not Found)

我确实计划构建这个端点,但我很好奇为什么当我可以看到ember存储中的数据时,它会查看API。

您正在查找store.filter,但没有会触发网络调用的query参数。

export default Ember.Route.extend({
    model: function(params) {
        return this.store.filter('vendor', function(vendor) {
            return vendor.get('slug') === params.slug;
        });
    }
});

顺便说一句,这将使用与过滤器匹配的新记录来更新模型,无论它们可能被添加到存储中。