gpt4 book ai didi

ember.js - Ember 数据处理服务器错误

转载 作者:行者123 更新时间:2023-12-02 04:59:44 26 4
gpt4 key购买 nike

我将 ember-data 0.13 与 rails 一起使用。我有一个 basicinfo Controller 来处理 basicinfo 模型更新。更新 Action 是:

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

基本信息.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>

当我第一次点击带有无效数据的更新按钮时,错误正确显示,但如果我不修复错误并再次按下更新按钮,Ember 会显示:“ Uncaught Error :尝试处理事件 willCommit on while状态为 rootState.loaded.updated.invalid. Called with undefined "如何解决 谢谢!

最佳答案

Ember 数据在处理错误时似乎有点问题。

我建议您执行以下操作:

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

在我看来,更好的解决方案是根据 record.isError 标志禁用更新按钮。

另一件需要考虑的事情是当服务器返回错误并且你想转换到另一个路由(比如使用取消按钮)时该怎么做。 Ember 数据会禁止你这样做,提示记录有 inFlightAtributes。在这种情况下,您可以再次调用 record.rollback() 将标志返回到初始状态并继续您的转换。

关于ember.js - Ember 数据处理服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17667837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com