【问题标题】:Ember - after saving model, how to get id in success callback from promiseEmber - 保存模型后,如何从承诺中获取成功回调中的 id
【发布时间】:2015-01-30 12:19:18
【问题描述】:

Ember-1.9.1 中的简单场景,Ember-Data 1.0.0-beta.14.1:

模板形式:

form
  fieldset
    dl
      dt: label First Name:
      dd: view Ember.TextField value=fields.first_name classNames='form-control'

    dl
      dt: label Last Name:
      dd: view Ember.TextField value=fields.last_name classNames='form-control'

    dl
      dt: label Sex:
      dd: view Ember.Select content=App.Patient.SEX prompt='Select a sex.' value=fields.sex classNames='form-control'

    dl
      dt: label Date of Birth:
      dd: view Ember.TextField value=fields.date_of_birth type="date" classNames='form-control'

    dl
      dt: label Insurance Provider:
      dd {{view Ember.Select content=insuranceProviders prompt='Select an insurance provider.' optionLabelPath='content.name' optionValuePath='content.id' value=currentInsuranceProvider.id classNames='form-control'}}
    dl
      dt: label Insurance Policy Number:
      dd: view Ember.TextField value=fields.insurance_policy_number classNames='form-control'

  fieldset.actions
    input type='submit' value='Create Patient' click="createPatient" class='btn btn-default'
    '
    a.cancel href="#" click="cancel" cancel

控制器动作:

createPatient: function() {
  var self = this;
  var fields = this.get('fields')
  fields.insurance_provider_id = this.currentInsuranceProvider.id

  if (App.Patient.valid(fields)) {
    var patient = this.store.createRecord('patient', fields)

    patient.set('date_of_birth', new Date(moment(fields.date_of_birth).format()));

    patient.set('insurance_provider', this.insuranceProviders.findBy('id', this.currentInsuranceProvider.id));

    patient.save().then(function(savedPatient){
      debugger
      self.transitionToRoute('patient', savedPatient);
    }).catch(failure);

    function failure(reason) {
      this.set('showError', true);
    }
  } else {
    this.set('showError', true)
  }
}

显示“患者”路线:

App.PatientRoute = Ember.Route.extend({
   model: function(params) { return this.store.find('patient', params.id); }
})

在 JS 控制台中返回:

> savedPatient

< Class {id: null, store: Class, container: Container, _changesToSync: Object, _deferredTriggers: Array[0]…}

> savedPatient.get('id')

< null

为什么我无法在成功回调(调试器语句所在的位置)中获取已保存模型的 ID?它确实转换成功,但路由中的 id 为空。如果您想在过渡后编辑新创建的模型,这会导致问题。

还尝试在回调中重新加载 savedModel,如果没有模型的 id,这实际上是不可能的。似乎在同步页面刷新时重新加载 Store 之前 id 不可用,使用服务器和模型的服务器 id 更新它。这怎么可能在回调中更新商店?

更新

答案:Rails 后端未呈现正确的 JSON。

def create
  patient = Patient.create(patient_params)
  render json: { :patient => [patient] }
end

【问题讨论】:

  • 你能在 JS Bin 中复制这个吗?保存有效载荷中返回什么?能分享一下路线代码吗?我看不出它为什么不起作用的任何原因。
  • 恕我直言 savedModel.get('id') 应该可以工作...不应该是 .catch(failure.bind(this) 吗?并且没有 Ember-1.9.2 :)
  • 您的后端在POST 响应中返回的id 是否正确?
  • @andrusieczko ,很明显,在 POST 响应之后,ID 正在后端的 SQL 中设置。我认为 ember 承诺会返回后端分配的 id?但似乎不是……问题就出在这里。
  • 只要不将id返回给前端,它就不会更新记录;我会写一个答案

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


【解决方案1】:

如果您执行patient.save(),则发送POST 请求。如果您想以标准方式执行此操作,那么在将记录持久化到数据库中之后,您应该返回 Ember 可以理解的有效响应。

POST /patients 响应示例:

{
  "patient": [{
    "id: "newly-created-id",
    // all the rest of the fields
  }]
}

您仍然可以以不同的方式返回它,但是您应该使用 Ember 的序列化程序对其进行转换。但是,您必须在回复中提供id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2014-09-14
    • 2020-07-11
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多