【问题标题】:ember-data miserable bug duplicate recordember-data惨bug重复记录
【发布时间】:2014-08-14 15:22:36
【问题描述】:

我正在使用 ember-data#canary,它在从商店中查找记录时出现严重错误。

File: router.js

this.resource('games', function() {
    this.route('game', { path: '/:game' });
});

File: games_route.js

App.GamesRoute = Ember.Route.extend({
    model: function() {

        // request: GET /api/games
        // response: { games: [
        //               { id: 1, slug: lym, name: lose your marbles }
        // ] }
        return this.store.find('game');
    }
});

File: game_route.js

App.GamesGameRoute = Ember.Route.extend({
    model: function(params) {
        // this query causes bogus data to show up.
        // request: GET /api/games/lym
        // response: { game: { id: 1, slug: lym, name: lose your marbles } }
        return this.store.find('game', params.game);

    },

    serialize: function(game) {
        return { game: game.get('slug') };
    }
});

File game_model.js

App.Game = DS.Model.extend({
    slug: DS.attr('string'),
    name: DS.attr('string'),
});

当我访问 /games ember-inspector 数据选项卡时显示 1 个游戏已加载。

`id`    `slug`    `name`
 1       lym       lose your marbles

当我访问 /games/lym ember-inspector 数据选项卡时显示 2 个游戏已加载。

`id`    `slug`         `name`
 1       lym            lose your marbles
 lym     undefined      undefined

第二个数据显然是伪造的。我不知道它是从哪里来的,这给我带来了麻烦。

【问题讨论】:

    标签: javascript ruby-on-rails node.js ember.js ember-data


    【解决方案1】:

    当您调用 this.store.find('game', params.game) 时,ember 数据认为 lym 是一个 id 并使用此 id 创建空记录。然后它接收带有实际记录的有效负载并以 id=1 进行存储。

    基本上,在store.find 中,您应该使用实际 id,而不是 slug,即使您的 API 支持 slug。

    【讨论】:

    • 非常感谢,现在我明白了这个问题,对于解决方案,我可以在路由上使用serializedeserialize钩子,我想他们可能会解决问题。跨度>
    • 现在我需要在 url 中使用 slug,在 model 钩子中 params 是 slug,我不能在 store.find 中使用实际 id 因为我有 slug 而不是 id .我该如何克服这个?有store.findBySlug这样的方法吗?
    • this.store.find('game', { slug : params.game })
    • 啊哈,我试过了,它请求查询参数/games?slug='bla',但我需要/games/bla
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2013-01-22
    • 2015-04-09
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多