简体   繁体   English

ember-data句柄服务器错误

[英]ember-data handle server error

I am using ember-data 0.13 with rails. 我正在使用带有rails的ember-data 0.13。 I have a basicinfo controller to handle basicinfo model update. 我有一个basicinfo控制器来处理basicinfo模型更新。 update action is: 更新动作是:

  update: ->
    @content.save()
    @content.on('becameInvalid', (response) ->
      alert Em.inspect(response.errors)
    )

basicinfo.hbs: basicinfo.hbs:

<aside class='basicinfo-aside'>
  {{#if inEditModel}}
<div class='control-group'>
  <label for='basicinfo_about_me'>{{t '.basicinfo.edit.about_me'}}</label>

  <div class='controls'>
    {{view Em.TextArea id='basicinfo_about_me'
                       class='basicinfo-about-me'
                       name='basicinfo[about_me]'
                       valueBinding='aboutMe'}}
  </div>
</div>

<div class='action-group'>
  <span {{bindAttr class=':about-me-length-remain
                          hasAboutMeLengthRemain:muted:text-error'}}>
    {{aboutMeLengthRemain}}
  </span>

  <button class='btn-cancel btn' {{action cancel}}>
    {{t '.basicinfo.edit.cancel'}}
  </button>

  <button class='btn-update btn btn-primary' {{action update}}>
    {{t '.basicinfo.edit.update'}}
  </button>
</div>

  {{/if}}
</aside>

<div class='basicinfo-inner'>
  {{#unless inEditModel}}
    <h5>
      {{t '.basicinfo.about_me'}}

      {{#if canManage}}
        <a class='lnk-edit' href='#' {{action edit}}>
          <i class='icon-edit'></i>
        </a>
      {{/if}}
    </h5>

    <p class='about-me'>{{aboutMe}}</p>
  {{/unless}}
</div>

when I click update button with invalid data first time the error shows properly, but if I dont fix error and press update button again Ember shows: "Uncaught Error: Attempted to handle event willCommit on while in state rootState.loaded.updated.invalid. Called with undefined " How to solve it Thanks! 当我点击无效的数据第一次更新按钮错误显示正确,但如果我不修复错误,按更新按钮再次灰烬表示:“未捕获的错误:试图处理事件willCommit上,而在状态rootState.loaded.updated.invalid。调用未定义的“如何解决它谢谢!

Ember data seems to be a little buggy when handling errors. 处理错误时,Ember数据似乎有点儿麻烦。

I would suggest you'd the following: 我建议你做以下事情:

update: ->
  @content.rollback() if @content.get('isError')
  @content.save().then ((success_responce)->
    <handle success responce here>
  ), (failure)->
    <handle failure here>

Still a better solution in my opinion would be to disable the update button , based on the record.isError flag. 在我看来,更好的解决方案是根据record.isError标志禁用更新按钮。

Another thing to consider is what to do when the server returns errors and you want to transition to another route (like with a cancel button). 另一件需要考虑的事情是当服务器返回错误并且您想要转换到另一条路线时(如使用取消按钮)该怎么办。 Ember data will forbid you to ,complaining the record has inFlightAtrributes. Ember数据将禁止你,抱怨记录有inFlightAtrributes。 In this case you can again call record.rollback() to return the flags to initial state and continue your transition. 在这种情况下,您可以再次调用record.rollback()将标志返回到初始状态并继续转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM