【问题标题】:Response from the server when saving a record to set the id ember-data保存记录以设置 id ember-data 时来自服务器的响应
【发布时间】:2013-10-17 13:22:11
【问题描述】:

在我使用 selection.save() 保存记录后,选择是我的模型,模型上没有设置 id。我当前的服务器响应与 show 的 json 相同。即:selection: {id: <id>} 和其他字段。 ember 数据本地存储中未设置购买 ID。 我使用默认的 RESTAdapter 和 Rails 作为后端。 (我将 jbuilder 用于 JSON)

模型是MatchPlayerSelection,一旦我保存记录,这是我发回的响应:

{"match_player_selection":{"id":41298,"user":3,"scheduledMatch":133,"matchPlayerScores":[3697,3696,3694,3698,3704,3719,3712,3709,3717,3707,3714],"captain":3694,"viceCaptain":3709}

【问题讨论】:

  • 您使用的是什么 REST 适配器?你用的是什么后端?你能展示你的生产json的sn-p吗?它是关系的一部分还是图表中的顶级对象?
  • 它是顶级对象。我已经用更多细节更新了这个问题。
  • 我目前面临同样的问题。您是否能够解决问题?
  • 我无法解决并做了一些解决方法,后来没有重新访问:(

标签: ember.js ember-data


【解决方案1】:

您如何测试id 的设置? save 函数是异步的并返回一个promise,因此您需要在检查id 之前解析promise。

这不起作用:

selection.save();
console.log( selection.get('id') );

这将起作用:

selection.save().then(function(savedSelection){
  console.log( savedSelection.get('id') );
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2013-04-16
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多