简体   繁体   中英

ember-data handle server error

I am using ember-data 0.13 with rails. I have a basicinfo controller to handle basicinfo model update. update action is:

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

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!

Ember data seems to be a little buggy when handling errors.

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.

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. In this case you can again call record.rollback() to return the flags to initial state and continue your transition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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